我试图弄清楚如何将信息从一种方法传递给main方法。它可能很简单,我只是遗漏了一些东西。请帮忙:)
public static void main(String[] args) throws IOException {
String[] name = stateName();
public static String[] stateName() throws IOException{
int numState =0;
File file = new File ("stateNames.txt");
Scanner in = new Scanner(file);
while (in.hasNextLine()){
String state = in.nextLine();
numState++;
}
String[] states = new String [numState];
Scanner reader = new Scanner(file);
for (int i= 0; i < numState; i++){
states[i] = reader.nextLine();
}
return states;
}
当我去运行时,我回来了:[Ljava.lang.String; @ 18892539我做错了什么?
答案 0 :(得分:0)
实际上,没有错。这正是它应该起作用的方式。
当您尝试使用System.out.println
打印数组时,您会得到您提到的那种奇怪的神秘内容。您可以使用
System.out.println(Arrays.toString(name));
而不是
System.out.println(name);
答案 1 :(得分:0)
其他方法
String[] name = {"John", "Mike", "Jim"};
// this out put will be like this
[ John, Mike, Jim ]
System.out.prinln(Arrays.asList(name));
// this way is better for formatting. Prints:
John
Mike
Jim
for (String s : name){
System.out.println(s);
}
// or this will print out
John Mike Jim
System.out.println(Arrays.toString(name));