2013-04-28 140 views
0

我在介紹編程類和我們的任務是創建一個程序,分析文件中提供的名稱列表Names.txt這裏是我到目前爲止的代碼(減去一個單獨的Java文件與類名的一些方法),其次是我的編譯錯誤,任何幫助表示讚賞!文件不會編譯name.java

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 of the 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));    
      }   
     } 
     while (!selection.equalsIgnoreCase("d")); 
    } 

    public 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); 
      } 
     } 
     return name; 
    } 
    public 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] == Integar.parseInt(this)) 
      { 
        top = top + babyName + "(" + this + ")" + "  "; 
      } 
      return top; 
     } 
    } 
}' 


NameApp.java:27: cannot find symbol 
symbol : method findName() 
location: class java.lang.String 
       name = nameIn.findName(); 
          ^
NameApp.java:34: cannot find symbol 
symbol : method findName() 
location: class java.lang.String 
       name = nameIn.findName(); 
          ^
NameApp.java:37: cannot find symbol 
symbol : method findName() 
location: class java.lang.String 
       name2 = nameIn.findName(); 
          ^
NameApp.java:74: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(first.findRank(decade)); 
             ^
NameApp.java:75: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(second.findRank(decade)); 
             ^
NameApp.java:76: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(third.findRank(decade)); 
             ^
NameApp.java:77: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(fourth.findRank(decade)); 
             ^
NameApp.java:78: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(fifth.findRank(decade)); 
             ^
NameApp.java:79: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(sixth.findRank(decade)); 
             ^
NameApp.java:80: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(seventh.findRank(decade)); 
             ^
NameApp.java:81: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(eighth.findRank(decade)); 
             ^
NameApp.java:82: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(ninth.findRank(decade)); 
             ^
NameApp.java:83: cannot find symbol 
symbol : method findRank(int) 
location: class java.lang.String 
       System.out.println(tenth.findRank(decade));    
             ^
NameApp.java:113: cannot find symbol 
symbol : method equalsIgnoreCase(java.lang.String) 
location: class NameApp 
      if(this.equalsIgnoreCase(babyName)) 
       ^
NameApp.java:118: cannot find symbol 
symbol : variable name 
location: class NameApp 
     return name; 
      ^
NameApp.java:145: cannot find symbol 
symbol : variable Integar 
location: class NameApp 
      if(popularity[decade - 1] == Integar.parseInt(this)) 
             ^
16 errors 
+0

檢查編譯類是在同一文件夾中的Java文件 – rozar 2013-04-28 03:40:04

+0

嘛加一些細節,比如你得到的編譯錯誤。 – GRAYgoose124 2013-04-28 03:55:38

回答

0

您的方法findName(),findRank()等屬於您的類NameApp。

但是在main()中,你在「String」類中調用了這些方法 - 這正是編譯錯誤所說的。 您需要安裝類似於 的名稱NameApp nameApp = new NameApp(); nameApp.findName()

哦,你拼寫Integer錯了。

使用像Eclipse的編輯器,它會幫助你解決這些問題

+0

我在哪裏放置該代碼NameApp nameApp = new NameApp(); 也可以給我一個鏈接下載eclipse? – user2328244 2013-04-28 05:01:43

+0

* user2328244 *我敢打賭,你可以通過在網上搜索諸如「eclipse download」之類的東西來找到Eclipse下載的鏈接。 – 2013-04-28 10:59:36