这是我的代码,用于读取具有多行的文件,每行包含字符串和浮点数并打印出每一行。我一直得到错误找不到方法split()和loadStrings()的符号。 我做错了什么?
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]);
}
答案 0 :(得分: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()] );
}