在 java 中传入 args[] 或缺少 args
passing in args[] or missing args in java
我正在为 class 中的作业编写代码,并且从未以这种方式实际使用过扫描器。然而,当我朗读下面的代码时,我得到了错误
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at World.<init>(World.java:20)
at World.main(World.java:58)
我想我只是很困惑,因为我有 eclipse 使用世界文本文件来创建,它只是不允许它。
import java.util.Scanner;
/*************************************************************************
* Name :
* Username :
* Description : *************************************************************************/
public class World{
//instance variables
String mapFile; //This holds the .txt name that contains the map.
Tile[][] worldMap;
public World(String mapFile){
this.mapFile = mapFile;
Scanner scanner = new Scanner(mapFile);
//get the dimensions of the world
int width = scanner.nextInt();
int height = scanner.nextInt();
int x_coor = scanner.nextInt();
int y_coor = scanner.nextInt();
//initialize the array to proper height and width
worldMap = new Tile[width][height];
//set the starting locations of the avatar character
Avatar avatar = new Avatar(x_coor, y_coor);
//populate the worldMap
for(int i = 0; i < worldMap.length - 1; i++){
for(int j = 0; i <worldMap[0].length -1; j++){
worldMap[i][j] = new Tile(scanner.next());
}
}
}
//draw the map
public void draw(){
for(int i = 0; i < worldMap.length - 1; i++){
for(int j = 0; j < worldMap[0].length -1; i++){
worldMap[i][j].draw(i, j);
}
}
}
// Test method
public static void main(String [] args)
{
World world = new World(args[0]);
world.draw();
}
}
您围绕 String
(而不是 File
)构建了 Scanner
。改变这个
Scanner scanner = new Scanner(mapFile);
至
Scanner scanner = new Scanner(new File(mapFile));
现在 Scanner
正在扫描您的 String
文件名。
我正在为 class 中的作业编写代码,并且从未以这种方式实际使用过扫描器。然而,当我朗读下面的代码时,我得到了错误
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at World.<init>(World.java:20)
at World.main(World.java:58)
我想我只是很困惑,因为我有 eclipse 使用世界文本文件来创建,它只是不允许它。
import java.util.Scanner;
/*************************************************************************
* Name :
* Username :
* Description : *************************************************************************/
public class World{
//instance variables
String mapFile; //This holds the .txt name that contains the map.
Tile[][] worldMap;
public World(String mapFile){
this.mapFile = mapFile;
Scanner scanner = new Scanner(mapFile);
//get the dimensions of the world
int width = scanner.nextInt();
int height = scanner.nextInt();
int x_coor = scanner.nextInt();
int y_coor = scanner.nextInt();
//initialize the array to proper height and width
worldMap = new Tile[width][height];
//set the starting locations of the avatar character
Avatar avatar = new Avatar(x_coor, y_coor);
//populate the worldMap
for(int i = 0; i < worldMap.length - 1; i++){
for(int j = 0; i <worldMap[0].length -1; j++){
worldMap[i][j] = new Tile(scanner.next());
}
}
}
//draw the map
public void draw(){
for(int i = 0; i < worldMap.length - 1; i++){
for(int j = 0; j < worldMap[0].length -1; i++){
worldMap[i][j].draw(i, j);
}
}
}
// Test method
public static void main(String [] args)
{
World world = new World(args[0]);
world.draw();
}
}
您围绕 String
(而不是 File
)构建了 Scanner
。改变这个
Scanner scanner = new Scanner(mapFile);
至
Scanner scanner = new Scanner(new File(mapFile));
现在 Scanner
正在扫描您的 String
文件名。