Results 1 to 13 of 13
- 02-07-2015, 07:15 PM #1
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Minor nuisance with exception output.
Nothing big, it's just that the way my exception is getting outputted. I have a menu like this:
1. blah blah
2. blah blah
3. blah blah
4. blah blah
and the exception would sometimes output
here
1. blah blah
2. blah blah
3. blah blah
4. blah blah
1. blah blah
here
2. blah blah
3. blah blah
4. blah blah
or
1. blah blah
2. blah blah
here
3. blah blah
4. blah blah
That can get quite annoying. How you suppose I fix it.
Here is a sample of my code.
Java Code:public static void main(String[] args) { ComputerInventory process = new ComputerInventory(); ArrayList<Computer> computer = new ArrayList<>(); int option; do { try { System.out.println("1.) Add computers to your inventory."); System.out.println("2.) Display your Inventory."); System.out.println("3.) Remove Computers from your inventory."); System.out.println("4.) Display the total cost of your inventory."); System.out.println("5.) Quit the program. "); option = read.nextInt(); switch (option) { case 1: process.addComputers(computer); System.out.println(""); break; case 2: process.displayInventory(computer); System.out.println(""); break; case 3: process.removeComputer(computer); System.out.println(""); break; case 4: process.totalCost(computer); System.out.println(""); break; case 5: System.out.println("\nThank you for using my program."); return; default: System.out.println("\nThis choice doesn't exist in the menu."); } } catch(InputMismatchException e) { System.err.print("Data does not match expected output." ); System.err.print(" Run the program again.\n\n" ); read.reset(); read.nextLine(); } } while (true); }
- 02-07-2015, 07:28 PM #2
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
Basically what I'm asking is why is the exception message moving all over the place when it should only print out where I want it to print out.
- 02-07-2015, 07:57 PM #3
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
Could it be that the user is simply entering data too fast and the program can't keep up with the user's input?
- 02-07-2015, 07:59 PM #4
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
Oh and while I was testing, I found another bug. This bug occurs if I enter something like this: 1 1 f. I think it's because the Scanner simply reads the number 1 and discards the rest of the data.
Last edited by Deathslice; 02-07-2015 at 08:04 PM.
- 02-07-2015, 08:03 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Minor nuisance with exception output.
I'm not certain about this, but stdout is buffered and stderr is not. So what happens when you replace System.err with System.out? Or do a System.out.flush() after each print.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-07-2015, 08:07 PM #6
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
It seems like it worked but it doesn't make sense to just print out. Wouldn't it make more sense to print it out as a error message?
I also addressed another bug in my previous post.
- 02-07-2015, 08:22 PM #7
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
I guess it's not a big deal .out instead .err because they are stilling going to read that error message. I guess my biggest concern is that the program accepts (1 f) as valid input mostly because the scanner only reads integers until it gets to the first white space. That also for any other read.nextInt() or double that I have in the res of my program.
Last edited by Deathslice; 02-07-2015 at 08:28 PM.
- 02-07-2015, 08:29 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Minor nuisance with exception output.
What happens if you do a System.out.flush() prior to printing the errors to System.err? Regarding your other bug, if you enter
1 2 3 4 5 f
Then each time nextInt is called, it will return the next int (assuming the delimiter hasn't changed). When it encounters the f, it will throw an exception.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-07-2015, 08:36 PM #9
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
it stills goes all over the place the place even if I use System.out.flush() before or after the error message when entering data really fast.
- 02-07-2015, 08:37 PM #10
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
Actually scratch that, it does it regardless if I input data fast or not.
- 02-07-2015, 08:50 PM #11
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
So using System.out.print is the only method that works for me
- 02-07-2015, 08:56 PM #12
Member
- Join Date
- Jan 2015
- Location
- Miami, FL
- Posts
- 86
- Rep Power
- 0
Re: Minor nuisance with exception output.
Well I guess it's not a big deal anyways. Another thing that is troubling me though is that every time I catch an exception, it diverts back to the beginning of the program. What do I have to do to make the program restart where it left off(Keep in mind that I have multiple methods and not everything in the main method).
- 02-07-2015, 09:24 PM #13
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Minor nuisance with exception output.
BTW, you probably only get this effect in Eclipse (or perhaps another IDE). If you run it in an MSDOS command window, everything is one color and in order.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
minor major exception
By curious_programmer in forum New To JavaReplies: 1Last Post: 05-13-2012, 10:36 PM -
Need help with this minor problem.
By juxta in forum New To JavaReplies: 12Last Post: 04-30-2012, 11:16 PM -
Is that command prompt necessary or a nuisance?
By blackbird in forum AndroidReplies: 0Last Post: 08-11-2011, 11:54 PM -
Minor of a matrix
By sehudson in forum New To JavaReplies: 3Last Post: 02-21-2011, 08:12 PM
Bookmarks