Search code examples
javastringjava.util.scannerequals

Comparing Strings while reading a file with Scanner .hasNext(), .next


I am trying to make a little Highscore for my Programm. The programm has a LogFile, i want to read that file and determine the winner. Because my programm has a few issues, you can answer the same question over and over again, and get credits for it. With the code bellow I tried to prevent a cheated score. I tried to compare the third, fourth and fifth String, which represents the calculation you have to solve. I want to ignore the count of the highscore when the next line has the same calculation Can someone help me with this ? I just get a bunch of errors.

Thanks for your time and help.

public int readFile() {
    String score = null;
    while (scan.hasNext()) {
        String a = scan.next();
        String b = scan.next();
        String c = scan.next();
        String d = scan.next();
        String e = scan.next();
        String f = scan.next();
        String g = scan.next();
        String h = scan.next();
        String i = scan.next();

        if (c.equals(scan.next()) && d.equals(scan.next()) && e.equals(scan.next())) {
        } else {
            temp = Integer.parseInt(i);
            if (temp > highscore) {
                highscore = temp;
            }
        }

    }
    return highscore;
}

Textfile :

    014/07/29 08:25:15  95 + 80 4       false       -171        0
    2014/07/29 08:25:15 95 + 80 4       false       -171        0
    2014/07/29 08:25:49 8 * 3   24      true        0       1
    2014/07/29 08:25:49 8 * 3   24      true        0       2
    2014/07/29 08:25:49 8 * 3   24      true        0       3
    2014/07/29 08:25:50 8 * 3   24      true        0       4
    2014/07/29 08:25:50 8 * 3   24      true        0       5

Errors:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2014/07/28"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at mathe.Highscore.readFile(Highscore.java:42)
    at mathe.Gui$TestActionListener.actionPerformed(Gui.java:231)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Solution

  • If you keep using scan.next in your conditional statements, your scanner will scan for the next value. I think you want to get the next value and use for your comparison instead.

    public int readFile() {
        String score = null;
        while (scan.hasNext()) {
            String a = scan.next();
            String b = scan.next();
            String c = scan.next();
            String d = scan.next();
            String e = scan.next();
            String f = scan.next();
            String g = scan.next();
            String h = scan.next();
            String i = scan.next();
    
            String nextVal = scan.next();
    
            if (c.equals(nextVal) && d.equals(nextVal) && e.equals(nextVal)) {
            } else {
                temp = Integer.parseInt(i);
                if (temp > highscore) {
                    highscore = temp;
                }
            }
    
        }
        return highscore;
    }
    

    I hope this helps. Cheers..!