2017-10-22 113 views
1

這是我的代碼,旨在讀取具有多行的文件,每行包含字符串和浮點數並打印出每行。我一直得到一個錯誤無法找到方法split()和loadStrings()的符號。 我在做什麼錯?處理找不到符號錯誤java

import java.util.Scanner; 
import java.io.FileNotFoundException; 
import java.io.File; 
import java.util.SplittableRandom; 
public class EmployeePay { 
    public static void main(String[] args) throws FileNotFoundException { 
    if (args.length != 1) { 
     final String msg = "Usage: EmployeePay name_of_input file"; 
      System.err.println(msg); 
      throw new IllegalArgumentException(msg); 
      } 

     final String inputFileName = args[0]; 
     final File input = new File (inputFileName); 
     final Scanner scanner = new Scanner(input); 

     String line = " "; 
     String Identification = " "; 
     float WorkTime; 
     float MoneyPerHour; 
     int TotalDeductions= 0; 
     String[] lines, info; 
     float[] numberInfo; 
      lines = loadStrings(scanner); 
      info = new String [lines.length]; 
      numberInfo = new float [lines.length]; 
      String [] tokens; 
      for (int i =0; i < lines.length; i++){ 
      tokens = split(lines[i]," "); 
      info [i] = tokens[0]; 
      numberInfo[i]= Float.parseFloat(tokens[1]); 
      System.out.println(info[i]+" "+numberInfo[i]); 
      } 
+1

「split」定義在哪裏?你的'EmployeePay'類沒有這樣的方法 – MadProgrammer

回答

0

編譯器是對的,你用了2個未聲明的方法。我想,你想使用String.split()但不知道語法。在另一方面,你不爲你的loadStrings()方法提供源,所以它可能只是斷章取義,EG宣佈它非靜態的,並試圖從static main()使用它...

無論如何,修改了代碼因此請嘗試如下所示:

public static void main(final String[] args) throws FileNotFoundException 
{ 
    if (args.length != 1) 
    { 
     final String msg = "Usage: EmployeePay <name_of_input file>"; 
     System.err.println(msg); 
     throw new IllegalArgumentException(msg); 
    } 

    final Scanner scanner = new Scanner(new File(args[0])); 

    final String[] lines = loadStrings(scanner); 
    final String[] info = new String[lines.length]; 
    final float[] numberInfo = new float[lines.length]; 
    for (int i = 0 ; i < lines.length ; i++) 
    { 
     final String[] tokens = lines[i].split(" "); 
     info[i] = tokens[0]; 
     numberInfo[i] = Float.parseFloat(tokens[1]); 
     System.out.println(info[i] + " " + numberInfo[i]); 
    } 
} 

private static String[] loadStrings(final Scanner scanner) 
{ 
    final List<String> result = new ArrayList<>(); 
    while (scanner.hasNextLine()) 
    { 
     result.add(scanner.nextLine()); 
    } 
    return result.toArray(new String[result.size()]); 
}