Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By jim829

Thread: Minor nuisance with exception output.

  1. #1
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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);
        }

  2. #2
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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.

  3. #3
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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?

  4. #4
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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.

  7. #7
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Minor nuisance with exception output.

    Quote Originally Posted by Deathslice View Post
    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.
    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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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.

  10. #10
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Minor nuisance with exception output.

    Actually scratch that, it does it regardless if I input data fast or not.

  11. #11
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Minor nuisance with exception output.

    So using System.out.print is the only method that works for me

  12. #12
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default 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).

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. minor major exception
    By curious_programmer in forum New To Java
    Replies: 1
    Last Post: 05-13-2012, 10:36 PM
  2. Need help with this minor problem.
    By juxta in forum New To Java
    Replies: 12
    Last Post: 04-30-2012, 11:16 PM
  3. Is that command prompt necessary or a nuisance?
    By blackbird in forum Android
    Replies: 0
    Last Post: 08-11-2011, 11:54 PM
  4. Minor of a matrix
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 08:12 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
  •