2

I'm having some trouble here. Basically, I am given the line of input

5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4

I am splitting by the =, then adjusting the coordinates, adding them back to array rows and shifting them either forward or backward.

The problem is, as soon as I start my program, it throws an input mismatch exception and not sure why.

Here is my code:

public class Pirate {
    
    int singleCoordinateX;
        int singleCoordinateY;
        PrintStream out;
        String position;
        Coordinate coordinates;
        CoordinateRow newSplitStringRow;
        String splitString;
        CoordinateRow adjustedSplitStringRow;
        CoordinateRow newAdjustedSplitStringRow;
    
    Pirate() {
    out = new PrintStream(System.out);
    position = "front";
            newSplitStringRow = new CoordinateRow();
            adjustedSplitStringRow = new CoordinateRow();
            newAdjustedSplitStringRow = new CoordinateRow();
    }
    
void start() {

    Scanner newCoordinateInputData = UIAuxiliaryMethods.askUserForInput().getScanner();

    String singleLineInputString = newCoordinateInputData.nextLine();

    splitLineString(singleLineInputString);

    printCoordinateRow();
}


void splitLineString(String singleLineInputString) {

    String[] newSingleLineStringArray = singleLineInputString.split("=");

    for(int i = 0; i < newSingleLineStringArray.length; i++) {

        splitString = newSingleLineStringArray[i];
        
        Scanner splitStringScanner = new Scanner(splitString);
        
        newCoordinateRow(splitStringScanner);
        
        placeCoordinateRow(newCoordinateRow(splitStringScanner));
    }
        
}


CoordinateRow newCoordinateRow(Scanner splitStringScanner) {
    
    splitStringScanner.useDelimiter("\\s+");
    
    while(splitStringScanner.hasNext()) {
        
        String coordinateString = splitStringScanner.next();
        
        coordinates = convertCoordinates(coordinateString);
        
        newSplitStringRow.addSingleCoordinateToBack(coordinates);
        
    }
    
    return newSplitStringRow;
    
}

Coordinate convertCoordinates(String coordinateString) {
    
    Scanner coordinateScanner = new Scanner(coordinateString);
    
    coordinateScanner.useDelimiter(",");
    
    while(coordinateScanner.hasNext()) {
        
        singleCoordinateX = coordinateScanner.nextInt();
        
        singleCoordinateY = coordinateScanner.nextInt();
        
        coordinates = new Coordinate(singleCoordinateX, singleCoordinateY);
    }
    
    return coordinates;
}



CoordinateRow placeCoordinateRow(CoordinateRow adjustedSplitStringRow) {

    if(position == "front") {

        newAdjustedSplitStringRow.addCoordinateRowFront(adjustedSplitStringRow);

        position = "back";

    } else if(position == "back") {

        newAdjustedSplitStringRow.addCoordinateRowBack(adjustedSplitStringRow);

        position = "front"; 
    }

    return newAdjustedSplitStringRow;

}

void printCoordinateRow() {

    for(int i = 0; i < newAdjustedSplitStringRow.numberOfElements; i++) {

        out.printf("%d, %d\n", newAdjustedSplitStringRow.newCoordinateArray[i].coordinateX, newAdjustedSplitStringRow.newCoordinateArray[i].coordinateY);           
    }

}

Stack trace:

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Pirate.Pirate.convertCoordinates(Pirate.java:81)
    at Pirate.Pirate.splitLineString(Pirate.java:63)
    at Pirate.Pirate.start(Pirate.java:47)
    at Pirate.Pirate.main(Pirate.java:257)

Output I am getting:

6, 4
5, 5
9, 7
7, 3
4, 2
10, 6
5, 3
8, 6
10, 8
6, 5
8, 8
7, 5
7, 4
6, 4
5, 5
9, 7
7, 3
4, 2
10, 6
5, 3
8, 6
6, 4
5, 5
9, 7
6, 4
5, 5
9, 7
7, 3
4, 2
10, 6
5, 3
6, 4
5, 5
9, 7
7, 3
4, 2
10, 6
5, 3
8, 6
10, 8

Output I should be getting

6,5
8,8
7,5
7,4
8,6
6,4
5,5
9,7
7,3
4,2
10,6
5,3
10,8

I'm not sure what is happening.

7
  • 2
    Please add the exception stack trace, it says why. Also, some of your global members/variables are not shown in your code. Commented Nov 7, 2021 at 8:39
  • Done. I have added the exceptions. Commented Nov 7, 2021 at 8:43
  • Which line is line 81 of your program? The stack trace says it tries to read an int there but can’t. Commented Nov 7, 2021 at 8:52
  • that is nextInt Commented Nov 7, 2021 at 8:55
  • You are calling nextInt() twice. Which one of them? Commented Nov 7, 2021 at 8:58

1 Answer 1

2

It seems that there are two types of coordinate value delimiters, commas and blanks.

You should change

splitStringArrayScanner.useDelimiter(",");

to

splitStringArrayScanner.useDelimiter(",|\\s+");
Sign up to request clarification or add additional context in comments.

5 Comments

O that worked, but it seems to print out my output twice
This is the output I get vs what is expected. (EXPECTED) 6,5 8,8 7,5 7,4 8,6 6,4 5,5 9,7 7,3 4,2 10,6 5,3 10,8 (MINE) 6, 4 5, 5 9, 7 7, 3 4, 2 10, 6 5, 3 8, 6 10, 8 6, 5 8, 8 7, 5 7, 4 6, 4 5, 5 9, 7 7, 3 4, 2 10, 6 5, 3 8, 6 6, 4 5, 5 9, 7 6, 4 5, 5 9, 7 7, 3 4, 2 10, 6 5, 3 6, 4 5, 5 9, 7 7, 3 4, 2 10, 6 5, 3 8, 6 10, 8
I have re-edited my code, but I seem to be getting alot more output then i should. Any ideas?
Now your "Input Mismatch Exception problem with code" has been resolved, it's a good idea to repost it as another question.
Well I would if my other thread was not closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.