0

我正在尝试编译一个程序,但我不断收到一些以前从未见过的错误。这是我的程序:

import java.util.Scanner;
import java.io.FileReader; 
import java.io.*;

public class Project1
{
  public static void main (String [] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new FileReader("artists.txt") ); //read from file
    Scanner console new Scanner(System.in); //read from user input 


int sum = 0; //sum of revenue
String artist; //user input
int revenue = 0; //store yearly revenue


System.out.println("Please enter the name of a band/artist: ");
artist = console.next();   

String[] tokens = artist.split(" "); 

while(inFile.hasNext() )
{
  if (tokens == inFile.hasNext() )
  {  
    if (inFile.Next == (2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012) );
    {
      if (inFile.hasNext() )
      {
        revenue = inFile.nextInt();
        sum += revenue;
        System.out.print("sum");
      }
    }
  }
}

inFile.close();

我得到的错误如下:

Error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement [Line 13]
Error: Syntax error on token "==", Name expected after this token [Line 30]

如果有人碰巧看到任何其他错误,请随时指出。谢谢你。

4

2 回答 2

4

你在这里错过了一个任务:

Scanner console new Scanner(System.in);

和这个:

if (inFile.Next == (2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012) );

(撇开结尾的分号)

不是我在 Java 中能认出的任何东西。我会将其分解为更小的单元,并尝试让小组件一次运行一个,否则很容易将自己与大量编译器错误混淆。

于 2013-04-02T16:10:31.173 回答
1

您缺少一个赋值运算符=

Scanner console = new Scanner(System.in);

您将无法执行此操作:

if (inFile.Next == (2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012) );

我猜你想要:

if (revenue == 2005 || revenue == 2006 || ...)) {
   sum += revenue;
}

或者干脆

  if (revenue >= 2005 && revenue <= 2012) {
于 2013-04-02T16:10:23.050 回答