Results 1 to 17 of 17
Thread: Mismatch error thrown?
- 04-19-2011, 12:56 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
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.
with the input: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]); } } }
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.
- 04-19-2011, 12:59 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Post the Exact error message you receive(copy/paste, no paraphrasing)
- 04-19-2011, 01:20 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
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.
- 04-19-2011, 01:26 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Since I am lazy, would you mind highlighting line 124 in your initial code for me?
- 04-19-2011, 01:30 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
There you go, thanks.
- 04-19-2011, 01:33 AM #6
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.
- 04-19-2011, 01:40 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
and let me know if it works.Java Code:if(scan.hasNext()){ String s = scan.next(); shutOut = Integer.parseInt(s); }
- 04-19-2011, 01:48 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
Threw another mismatch at the same spot. :(
Im working on it Junky, I just don't really know where to begin at.
- 04-19-2011, 01:57 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
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".
- 04-19-2011, 02:03 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Using scan.next instead of scan.nextInt() also through the same error?
- 04-19-2011, 02:13 AM #11
Change your code as below and it will help work out what is wrong with your code.
This is what I meant by debugging your code which is your job not mine!Java Code:int count = 1; while(!nameF.equals(sent)) { System.out.println(count++); if(scan.hasNextInt()) //etc
- 04-19-2011, 02:20 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
- 04-19-2011, 02:27 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
Okay, so I was misunderstanding where my code failed.
I feel smart. :<
- 04-19-2011, 02:33 AM #14
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 + "<");
- 04-19-2011, 02:43 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
>NoPitcher <
For some reason, it finds an extra space after "NoPitcher". So soon as I placed my sentinel as "NoPitcher ", it worked.
- 04-19-2011, 02:46 AM #16
The reason is because you put it there.
Java Code:nameF += " ";
- 04-19-2011, 02:58 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Object mismatch, not sure why
By olddog in forum New To JavaReplies: 1Last Post: 01-29-2011, 10:18 PM -
Type Mismatch error
By and0rsk in forum New To JavaReplies: 2Last Post: 10-10-2010, 11:16 AM -
How many no. of exceptions can be thrown????
By Stephen Douglas in forum New To JavaReplies: 8Last Post: 04-30-2010, 05:12 PM -
Problem .... sockettimeoutException not thrown
By Shiv in forum NetworkingReplies: 0Last Post: 06-08-2009, 09:59 PM -
Which exception is thrown.....
By money123 in forum New To JavaReplies: 1Last Post: 07-30-2007, 03:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks