【问题标题】:Read an instruction line by line java逐行读取指令java
【发布时间】:2015-03-08 17:50:30
【问题描述】:

我在一个文本文件中有一组说明:

LoadA 0

LoadB 1

Add

Store 0

LoadA 2

etc...

我知道我可以使用 Scanner 和 hasNextLine 但不知道如何实现它并阅读并理解说明。

【问题讨论】:

  • SO 不是一个让人们做你的家庭作业的地方,Learn你自己,解决它,然后如果你遇到任何问题,这个社会总是在那里提供帮助
  • 先试试看教程!!
  • Neeraj Jain,我一直在努力解决它。我不知道如何实现它。我需要这一部分的帮助,大约占整个作业的 1/15。谢谢

标签: java file-io java.util.scanner


【解决方案1】:

尽管上面的人希望你自己做这件事,我会回答这个问题,因为我记得学习是多么困难。只要您从中学习而不只是复制它们,这应该很有用。

Scanner sc = new Scanner(System.in);   //read from the System.in
while (sc.hasNextLine()) { //this will continue to itterate until it runs out
     String[] x = sc.nextLine().split(" ");
     //this takes your input and puts it into a string array where there is a 
     //space e.g. ["LoadA", "0"]
}

我希望这会有所帮助。您仍然需要解决问题。您现在可以立即获取内容。祝你好运。

【讨论】:

  • 谢谢!!很有帮助
【解决方案2】:
Scanner inFile = null;
try 
    {
       // Create a scanner to read the file, file name is parameter
        inFile = new Scanner (new File("whatever.txt"));
        } 
        catch (FileNotFoundException e) 
        {
        System.out.println ("File not found!");
        // Stop program if no file found
        System.exit (0);
        }

那么,

while(inFile.hasNextLine()){
    (some variable) = inFile.nextLine();
    (do something to that variable);
}

如果这不能解决问题,我建议您查看http://www.cs.swarthmore.edu/~newhall/unixhelp/Java_files.html

【讨论】:

  • 感谢您的帮助。非常感谢
猜你喜欢
  • 1970-01-01
  • 2017-10-01
  • 2015-10-24
  • 2016-01-24
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
相关资源
最近更新 更多