What should I change to make this file compile?

2019-03-07 04:47发布

I am in a programming class and this program is part of my homework. This file analyzes data from a file called "Names.txt" and then prints the information. I'm getting compilation errors and I want to know what I need to change or add to make it compile successfully.

Here is my code:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class NameApp {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        String selection, nameIn, nameIn2;
        Name name, name2;
        int decade;
        String first = "1", second = "2", third = "3", fourth = "4", fifth = "5", sixth = "6", seventh = "7", eighth = "8", ninth = "9", tenth = "10";
        do {
            System.out.println("Enter the character corresponding to your selection:");
            System.out.println("a - Print histogram for a name");
            System.out.println("b - Compare two names in a decade");
            System.out.println("c - Print top ten names for a decade");
            System.out.println("d - Quit (display file anomalies)");
            selection = stdin.next();
            System.out.println("your selection: " + selection);
            if (selection.equalsIgnoreCase("a")) {
                System.out.println("Enter a name: ");
                nameIn = stdin.next();
                name = nameIn.findName();
                System.out.println("Histogram for name, " + name.getName());
            }
            if (selection.equalsIgnoreCase("b")) {
                System.out.println("Enter a name: ");
                nameIn = stdin.next();
                name = nameIn.findName();
                System.out.println("Enter a name: ");
                nameIn2 = stdin.next();
                name2 = nameIn.findName();
                System.out.println("Enter number corresponding to your decade:");
                System.out.println("1 - 1900-1909");
                System.out.println("2 - 1910-1919");
                System.out.println("1 - 1920-1929");
                System.out.println("1 - 1930-1939");
                System.out.println("1 - 1940-1949");
                System.out.println("1 - 1950-1959");
                System.out.println("1 - 1960-1969");
                System.out.println("1 - 1970-1979");
                System.out.println("1 - 1980-1989");
                System.out.println("1 - 1990-1999");
                System.out.println("1 - 2000-2005");
                System.out.println("Enter a decade: ");
                decade = stdin.nextInt();
                System.out.println("Data for " + name.getName());
                System.out.println(name.getHistoLine(decade));
                System.out.println("Date for " + name2.getName());
                System.out.println(name2.getHistoLine(decade));
            }
            if (selection.equalsIgnoreCase("C")) {
                System.out.println("Enter number corresponding to your decade:");
                System.out.println("1 - 1900-1909");
                System.out.println("2 - 1910-1919");
                System.out.println("1 - 1920-1929");
                System.out.println("1 - 1930-1939");
                System.out.println("1 - 1940-1949");
                System.out.println("1 - 1950-1959");
                System.out.println("1 - 1960-1969");
                System.out.println("1 - 1970-1979");
                System.out.println("1 - 1980-1989");
                System.out.println("1 - 1990-1999");
                System.out.println("1 - 2000-2005");
                System.out.println("Enter a decade: ");
                decade = stdin.nextInt();
                System.out.println("Ten most popular names (male and female) during the decade 1990-1999 were: ");
                System.out.println(first.findRank(decade));
                System.out.println(second.findRank(decade));
                System.out.println(third.findRank(decade));
                System.out.println(fourth.findRank(decade));
                System.out.println(fifth.findRank(decade));
                System.out.println(sixth.findRank(decade));
                System.out.println(seventh.findRank(decade));
                System.out.println(eighth.findRank(decade));
                System.out.println(ninth.findRank(decade));
                System.out.println(tenth.findRank(decade));
            }
            if (!selection.equalsIgnoreCase("a") || !selection.equalsIgnoreCase("b") || !selection.equalsIgnoreCase("C") || !selection.equalsIgnoreCase("d"))
                ;
            {
                System.out.println("Invalid input. Try again");
            }
        }
        while (!selection.equalsIgnoreCase("d"));
        if (selection.equalsIgnoreCase("d")) {
        }
    }




    public Static Name findName() {
        String fileName = "Names.txt";
        Scanner inputStream = null;
        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file" + inputStream);
            System.exit(0);
        }
        String line;
        while (inputStream.hasNextLine()) {
            line = inputStream.nextLine();
            String[] nameLine = line.split(" ");
            String babyName = nameLine[0];
            int[] popularity;
            for (int k = 0; k < nameLine.length; k++) {
                popularity[k] = Integer.parseInt(nameLine[k + 1]);
            }
            if (this.equalsIgnoreCase(babyName)) {
                Name name = new Name(babyName, popularity);
            } else {
                System.out.print("Sorry that name was not found");
            }
        }
        return name;
    }

    public Static String findRank(int decade) {
        String fileName = "Names.txt";
        Scanner inputStream = null;
        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening the file" + inputStream);
            System.exit(0);
        }
        String top = "";
        String line;
        while (inputStream.hasNextLine()) {
            line = inputStream.nextLine();
            String[] nameLine = line.split(" ");
            String babyName = nameLine[0];
            int[] popularity;
            for (int k = 0; k < nameLine.length; k++) {
                popularity[k] = Integer.parseInt(nameLine[k + 1]);
            }
            if (popularity[decade - 1] == Integer.parseInt(this)) {
                top = top + babyName + "(" + this + ")" + "        ";
            }
            return top;
        }
    }
}

Here are the compilation errors

NameApp.java:126: ';' expected
    public Static Name findName()
                      ^
NameApp.java:126: invalid method declaration; return type required
    public Static Name findName()
                       ^
NameApp.java:161: ';' expected
    public Static String findRank(int decade)
                        ^
NameApp.java:161: invalid method declaration; return type required
    public Static String findRank(int decade)
                         ^
4 errors

2条回答
虎瘦雄心在
2楼-- · 2019-03-07 05:11

Case sensitivity could be your issue. Check out Stack Overflow question Is Java case-sensitive?.

I would suggest altering the case of static:

public static Name findName()

A lot of your work seems wrong in this. For example, declarations of your strings.

查看更多
我只想做你的唯一
3楼-- · 2019-03-07 05:25

There are a lot of problems here. I strongly encourage you to revisit tutorials and sit down with either a tutor or TA to hammer out the core syntax understandings.

  • static is a keyword. It is case-sensitive. You must change all capitalized forms of static to lower case.

  • From what you have of your code, there isn't any class called Name. That's going to pose a huge problem.

  • nameIn is a String. It will not have a method called findName.

  • int[] popularity doesn't have any size defined to it. You will be attempting to dereference null when you go to add things to this array. Declare its size appropriately by new int[nameLine.length].

  • These lines give out false information; I presume you meant to change them to a natural numerical ordering instead of having 1 all the way down?

    System.out.println("1 - 1900-1909");
    System.out.println("2 - 1910-1919");
    System.out.println("1 - 1920-1929");
    System.out.println("1 - 1930-1939");
    System.out.println("1 - 1940-1949");
    System.out.println("1 - 1950-1959");
    System.out.println("1 - 1960-1969");
    System.out.println("1 - 1970-1979");
    System.out.println("1 - 1980-1989");
    System.out.println("1 - 1990-1999");
    System.out.println("1 - 2000-2005");
    
  • first, second, ..., tenth are all of type String. They do not have a method findRank.

I'm sure there's more, but I'm going to stop here. Once you get those sorted out, you'll likely be in prime position to fix the rest.

查看更多
登录 后发表回答