首页 > 解决方案 > 我的java代码中的未知语法

问题描述

我在while 循环结束之前index.hasNextinput.nextInt();之前收到语法错误。“input.nextInt();” 对于“权重[索引] = input.nextInt();” 适用,但不适用于“类型 [索引]”。

 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;
 public class Horse {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input =null;
    int[] weights = new int[100];
    String[] types = new String[100];

    try {
        input = new Scanner(new File("horseData.csv"));
    } catch (FileNotFoundException e) {
        System.out.println(" Error FIle Not FOund");
        e.printStackTrace();
        System.exit(1);
    }//end catch

    int index = 0;
    while (index.hasNext())     {
        weights[index] = input.nextInt();
        types[index] = input.nextInt();
        System.out.println(" Weight: "+ weights[index] + ", types:" + types[index]);
    }//end while

    input.close();

    for(int i = 0; i < types.length;i++)
        System.out.println((i+1) + "\t" + amountOfFeed(types[i], weights[i]));

}//end main

public static double amountOfFeed(String horseType, int horseWeight)    {
    if(horseType.equals(" maintance") || horseType.equals(" light work") )
        return horseWeight * .02;
    else if (horseType.equals(" Moderate work"))    
        return horseWeight * .025;

    return horseWeight * .03;

}


}

谢谢:)

标签: java

解决方案


更改以下内容

while (index.hasNext()) 

while (input.hasNext()) 

您可能还需要index使用index++


推荐阅读