我正在尝试将String值传递给接受字符串的方法,但是我收到错误“不兼容的类型”。我已经尝试过对一个int值进行硬编码,看看它给出的错误是什么:
找到int; required:java.lang.String。
将方法更改为接受文件而不是错误:
找到String;必需:java.io.File。
ergo,我正在喂一个字符串应该是的字符串。但我不明白我哪里错了。 (我已将其更改回Feed String并接受String)
欢迎任何反馈。提前谢谢:)
import java.io.*;
import java.util.*;
public class Test
{
private ArgsReader inputFile;
private String filename;
private List<Pallet> entryBayQueue;
/**
* Constructor for objects of class Test
*/
public Test()
{
inputFile = new ArgsReader();
entryBayQueue = new LinkedList();
}
/**
* methods
*/
public void run(String[] args)
{
if(args.length > 0) //launched if you gave an argument
{
String line;
filename = args[0];
System.out.println(filename.getClass().getSimpleName()); //outputs string
line = inputFile.stringFileReader(filename); //call method containing reading capability to read and store contents in line
// ******* here is where the error occurs
//System.out.println(line); //line = null here
StringTokenizer st = new StringTokenizer(line);//tokenize line's contents
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
int serialNum = 0;
switch(st.nextToken())
{
case "A":
{
serialNum++;
Pallet almondPallet = new Pallet(1,serialNum); //create new almond pellet and assign serial number
entryBayQueue.add(almondPallet); //adds pallet to the end of the list
break;
}
}
}
}
else //launched when you didn't provide an argument
{
filename = null;
Console console = System.console();
filename = console.readLine("file to read from: ");
inputFile.stringFileReader(filename); //call method containing reading capability
}
}
}
// ******this is the implementation of stringFileReader
public void stringFileReader(String filename)
{
try
{
input = new FileReader(filename); //open file for reading (string filename)
buffReader = new BufferedReader(input); // read a line at a time
line = buffReader.readLine(); //read 1st line
while (line != null)
{
lineNum++;
System.out.println(line);
line = buffReader.readLine(); //read next line
}
}
catch (IOException e){System.out.println("caught IOException");}
答案 0 :(得分:0)
public void stringFileReader(String filename)
应该是
public String stringFileReader(String filename)
当期待调用时,方法没有返回值。
感谢任何帮助过的人。