首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传入args[]或缺少java中的args

传入args[]或缺少java中的args
EN

Stack Overflow用户
提问于 2016-03-04 22:11:38
回答 1查看 58关注 0票数 0

我正在为课堂上的作业编写代码,从来没有这样使用过扫描仪。然而,当我读取下面的代码时,我会得到以下错误

代码语言:javascript
复制
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)

我想我只是有点困惑,因为我使用world文本文件创建了eclipse,它只是不允许这样做。

代码语言:javascript
复制
   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();       
}   

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-04 22:14:24

您的Scanner是围绕一个String (而不是围绕一个File)构建的。改变这个

代码语言:javascript
复制
Scanner scanner = new Scanner(mapFile);

代码语言:javascript
复制
Scanner scanner = new Scanner(new File(mapFile));

现在,Scanner正在扫描您的String文件名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35807042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档