我在编程课程中,这个课程是我作业的一部分。此文件分析名为“Names.txt”的文件中的数据,然后打印信息。我收到编译错误,我想知道我需要更改或添加以使其成功编译。
这是我的代码:
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;
}
}
}
以下是编译错误
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
答案 0 :(得分:1)
区分大小写可能是您的问题。查看Stack Overflow问题 Is Java case-sensitive? 。
我建议改变static
:
public static Name findName()
你的很多工作似乎都错了。例如,声明字符串。
答案 1 :(得分:0)
这里有一些很多的问题。我强烈鼓励您重新访问教程并与导师或TA坐下来敲定核心语法理解。
static
是一个关键字。它区分大小写。您必须将所有大写形式的static
更改为小写。
根据您的代码,没有任何名为Name
的类。这将构成一个巨大的问题。
nameIn
是String
。 不会有一个名为findName
的方法。
int[] popularity
没有定义任何大小。当您向此数组添加内容时,您将尝试取消引用null。通过new int[nameLine.length]
适当地声明其大小。
这些行提供虚假信息;我认为你的意思是将它们改为自然的数字排序,而不是让它一直向下?
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
都是String
类型。他们不有方法findRank
。
我相信还有更多,但我会在这里停下来。一旦你那些整理出来,你就可能处于主要位置来修复休息。