尝试扫描字符串,该字符串是已扫描文件的一行

时间:2016-03-07 19:03:17

标签: java text-files

我已经从文本文件中扫描了各行,现在需要将该行分成四个不同的字符串,然后将它们添加到数组中。我在这里做错了什么帮助?

public void method()throws FileNotFoundException{

    FileDialog fileBox = new FileDialog(mainWindow,"Open", FileDialog.LOAD);
    fileBox.setVisible(true);
    fileBox.setDirectory(".");
    String dataFile = fileBox.getFile();
    Scanner scanner = new Scanner(new File(dataFile));
    while(scanner.hasNext())
    {
        String lineOfInput = scanner.nextLine();
        if (lineOfInput.startsWith("/") || lineOfInput.startsWith("[") && lineOfInput !=null)
        scanner.nextLine();
        else
        {
            String newLineOfInput = lineOfInput.trim();
            System.out.println(newLineOfInput);
            Scanner newScanner = new Scanner(newLineOfInput);
            while(newScanner.hasNext())
            {
                String group = scanner.next();
                String vehID = scanner.next();
                String regNo = scanner.next();
                String make = scanner.next();
                storeVehicle( new Vehicle(group, vehID, regNo,make));
                newScanner.close();
            }
        }
        scanner.close();

    }           

2 个答案:

答案 0 :(得分:0)

刚刚浏览了问题......似乎下面代码中的scanner应该是newScanner

String group = scanner.next();
String vehID = scanner.next();
String regNo = scanner.next();
String make = scanner.next();

因为你想使用存储在变量newLineOfInput下的行;我假设?

更好的选择是使用String#split功能。您不会创建一个全新的Scanner对象。它会是这样的:

String[] lines = newLineOfInput.split(" "); // Splits the string into a string array separated at every 'Space' character.
String group = lines[0];
String vehID = lines[1];
...

答案 1 :(得分:0)

试试这个

    int IntSLList::frequency(int e)
    {

           int total = 0;
           IntSLLNode *temp;
           for (temp = head; temp!=0 && !(temp-> info ==e) ; temp = temp->next)
           {
                 total++;   
            }
          return total;

不确定这是否有帮助,但也许值得一试。

相关问题