Results 1 to 17 of 17
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Mismatch error thrown?

    When I run my program, it throws mismatch error when trying to read in the shutOut variable. I'm not really sure why.

    Java Code:
    import java.util.Scanner;
    
    public class PitcherData
    {
    public static void main(String[] args)
       {
          
          final String sent = "NoPitcher";
    
          
          final int arrSize = 100;
    
    
         
          int numRecords = 0;
          int iPos;
          int shutOut = 0;
          String nameF, nameL;
          double era = 0;
    
          
          Scanner scan = new Scanner(System.in);
    
          
          String[] pitchNameF = new String[arrSize];
          String[] pitchNameL = new String[arrSize];
          int[] shutOutArr = new int[arrSize];
    
          
          double[] eraArr = new double[arrSize];
    
          
    
          nameF = scan.next();
          if(nameF.equals(sent))
          {
             System.out.println("Error: Sentinel rearched before loop.");
             System.exit(0);
          }
          if(nameF.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
          if(nameF.length() < 20)
          {
             nameF += " ";
          }
          nameL = scan.next();
          if(nameL.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
          if(nameL.length() < 20)
          {
             nameL += " ";
          }
          System.out.println(nameF + " " + nameL);
         
          while(!nameF.equals(sent))
          {
    
             
            if(scan.hasNextInt())
            {
    [B][I][U][SIZE="6"]            shutOut = scan.nextInt();[/SIZE][/U][/I][/B]
            }
            else
            {
               System.out.println("shutOut in incorrect format."
                                  + "\nQuitting program.");
               System.exit(0);
            }
            if(scan.hasNextDouble())
            {
               era = scan.nextDouble();
            }
            else
            {
               System.out.println("era in incorrect format."
                                  + "\nQuitting program.");
               System.exit(0);
            }
             
    
                eraArr[numRecords] = era;
    
                pitchNameF[numRecords] = nameF;
    
                pitchNameL[numRecords] = nameL;
    
                shutOutArr[numRecords] = shutOut;
    
             
             numRecords++;
    
    
             nameF = scan.next();
             if(nameF.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
             if(nameF.length() < 20)
             {
                nameF += " ";
             }
             nameL = scan.next();
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
             if(nameL.length() < 20)
             {
                nameL += " ";
             }
    
    
          }
    
    
          
          printData(numRecords, pitchNameF, pitchNameL, eraArr, shutOutArr);
    
    
          
          nameF = scan.next();
          if(nameF.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
          nameL = scan.next();
          if(nameL.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
    
    
    
          while(!nameF.equals(sent))
          {
             
             if(scan.hasNextInt())
             {
                shutOut = scan.nextInt();
             }
             else
             {
                System.out.println("ERA in invalid format. \n Quitting program.");
                System.exit(0);
             }
            
             iPos = seqSearch(numRecords, pitchNameF, pitchNameL, shutOutArr,
                    nameF, nameL, shutOut);
    
             
             if(iPos > -1)
             {
                System.out.println("The student: " + nameF + " with a gpa of "
                                   + era + " was found in the array at position "
                                   + iPos + ".");
             }
             else
             {
                System.out.println("The student: " + nameF + " with a gpa of "
                                   + era + " was not found in the array.");
             }
             
             nameF = scan.next();
             if(nameF.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
             nameL = scan.next();
             if(nameL.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
    
    
          }
       } 
    
       public static int seqSearch(int numRecords, String[] pitchNameF,
          String[] pitchNameL, int[] shutOutArr,
          String nameKeyF, String nameKeyL, int shutOut)
       {
            
            for(int i = 0; i < numRecords; i++)
               if(pitchNameF[i].equals(nameKeyF) && pitchNameL[i].equals(nameKeyL)
                  && shutOutArr[i] == shutOut)
                  return i;
            return -1;
    
    
       } 
    
       public static void printData(int numRecords, String[] pitchNameF,
          String[] pitchNameL, double[] eraArr, int[] shutOutArr)
       {
          
          for(int i = 0; i < numRecords; i++)
          {
             System.out.println("Record Number: " + i
                                + " First Name: "
                                + pitchNameF[i] + " Last Name: " + pitchNameL[i]
                                + "Shut Out Games: " + shutOutArr[i]
                                + " ERA: " + eraArr[i]);
          }
       }
    }
    with the input:
    Java Code:
    Walter Johnson  110 2.17
    Greg Maddux 35 3.16
    Cy Young 76 2.63
    Chisty Mathewson 79 2.13
    NoPitcher
    Greg Maddux 35
    Tom Seaver 61
    NoPitcher
    Last edited by Teclis; 04-19-2011 at 01:30 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Post the Exact error message you receive(copy/paste, no paraphrasing)

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Well, with the if/else in when reading the shutOut in, it says:
    Walter Johnson
    shutOut in incorrect format.
    Quitting program.


    with out the if/else:
    -bash-3.2$ java PitcherData < PitcherData.in
    Walter Johnson
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:857)
    at java.util.Scanner.next(Scanner.java:1478)
    at java.util.Scanner.nextInt(Scanner.java:2108)
    at java.util.Scanner.nextInt(Scanner.java:2067)
    at PitcherData.main(PitcherData.java:124)
    Last edited by Teclis; 04-19-2011 at 01:22 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since I am lazy, would you mind highlighting line 124 in your initial code for me?

  5. #5
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    There you go, thanks.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Your problem may be related to a known side effect of using nextInt or nextDouble. Both of these methods do not consume an end of line (EOL) character. So the next time you call nextLine it does consume the EOL and returns an empty String. This puts your code and input data out of whack. I'm not going to debug this for you. That is your job. See if this is what is causing your problem.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The only thing I can think of is that it is not reading the next int because there is a space there after the last read operation. Try changing it to have this like
    Java Code:
    if(scan.hasNext()){
      String s = scan.next();
      shutOut = Integer.parseInt(s);
    }
    and let me know if it works.

  8. #8
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Threw another mismatch at the same spot. :(

    Im working on it Junky, I just don't really know where to begin at.

  9. #9
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    If I read your recommendation correctly, then I'm pretty sure it isn't what is wrong. For some reason, the code is reading in "Walter", "Johnson", but isn't reading in the following Int on the same line which is "110".

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Using scan.next instead of scan.nextInt() also through the same error?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Change your code as below and it will help work out what is wrong with your code.
    Java Code:
    int count = 1;
    while(!nameF.equals(sent)) {
        System.out.println(count++);
        if(scan.hasNextInt())
        //etc
    This is what I meant by debugging your code which is your job not mine!

  12. #12
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Change your code as below and it will help work out what is wrong with your code.
    Java Code:
    int count = 1;
    while(!nameF.equals(sent)) {
        System.out.println(count++);
        if(scan.hasNextInt())
        //etc
    This is what I meant by debugging your code which is your job not mine!
    What is this
    I dont even


    I would've never thought of that.

  13. #13
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Okay, so I was misunderstanding where my code failed.


    I feel smart. :<

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    To help you even further add these two lines of code at the very bottom of your while loop (but still inside);
    Java Code:
    System.out.println(nameF.equals(sent));
    System.out.println(">" + nameF + "<");

  15. #15
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    >NoPitcher <

    For some reason, it finds an extra space after "NoPitcher". So soon as I placed my sentinel as "NoPitcher ", it worked.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    The reason is because you put it there.
    Java Code:
    nameF += " ";

  17. #17
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

Similar Threads

  1. Object mismatch, not sure why
    By olddog in forum New To Java
    Replies: 1
    Last Post: 01-29-2011, 10:18 PM
  2. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  3. How many no. of exceptions can be thrown????
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-30-2010, 05:12 PM
  4. Replies: 0
    Last Post: 06-08-2009, 09:59 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •