得到错误,但我不确定他们的意思

时间:2013-04-02 16:07:53

标签: java compiler-errors

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

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]

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

2 个答案:

答案 0 :(得分:4)

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

Scanner console new Scanner(System.in);

和此:

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

(撇开尾部分号)

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

答案 1 :(得分: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) {