Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Handling Errors / calling Error-Catching Methods

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Handling Errors / calling Error-Catching Methods

    I have the following code, with code to catch various errors from user input.

    import java.util.Collections;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.ListIterator;
     
    public class Track 
    {
    public static int getNumber(String prompt, Scanner sc, int lo, int hi)
     {
           while (true)
           {
                   System.out.print(prompt);
                   System.out.flush();
                   String in= null;
                   try 
                   {
                           in = sc.nextLine();
                           int input= Integer.parseInt(in);
                           if (input >= lo && input <= hi)
                                   return input;
                           System.out.println(input+" is not in the range ["+lo+","+hi+"].");
                   }
                   catch (NumberFormatException nfe) {
                           System.out.println("'"+in+"' is not a number.");
                   }
                   catch (Exception e)
                   {
                           System.out.println("sorry, something went wrong.");
                   }
           }
    }
       public static void main (String[] args)
       {
           Scanner scan = new Scanner (System.in);
           ArrayList<Portfolio> tracks = new ArrayList<Portfolio>();
     
     
           //Defines the elements in the array
           String songTitle;
           String songDate;
           int songParts;
           int total = 0;
     
           //Defines the user input necessary to run the program. Action is in the do-while loop.
           String keepWriting = "y";
     
     
           //Do-while loop
           do
           {
                //Asks for the name of the composition and goes to next line.
               System.out.print ("Enter the name of the song or composition: ");
               songTitle = scan.nextLine();
     
               //Asks for the date at which the composition was created and goes to next line.
               System.out.print ("Enter the date: ");
               songDate = scan.nextLine();
     
               //Asks for the number of instrumental or miscellaneous parts in the song. 
               System.out.print ("Enter the number of parts in the song: ");
               songParts = scan.nextInt();       
               scan.nextLine();
     
               // Creates a new song and adds it to the tracklist
               Portfolio portfolio = new Portfolio(songTitle, songDate, songParts);
               tracks.add (portfolio);
     
               // Prints the contents of the tracks object using println
               total += (portfolio.getParts());//+portfolio.getParts());
               ListIterator iterator = tracks.listIterator();
                   while (iterator.hasNext())
     
                   System.out.println(iterator.next());
     
               //Asks if the user wishes to continue programming.
               System.out.print ("Continue composing (y/n)? ");
               keepWriting = scan.nextLine();
     
                    }
           //End of the do while loop. Continues the programming if "y" was inputed. Otherwise, prints out
           //all contents of the array.
     
           while (keepWriting.equals("y"));
           Collections.sort(tracks);
           System.out.println("\n \nThere are " + tracks.size() 
           + " songs in your tracklist." + "\nThe total number of instrumental parts are "  + total + "."
           +  "\n\nHere they are in order." + "\n" + tracks);
     
            System.out.println("Keep the tunes a'comin!");
              }
       }

    How would I, for example, call the getNumber method at the part where the program asks the user for in put for songParts?

    like this???
    System.out.print ("Enter the number of parts in the song: ");
    songParts = scan.nextInt();
    songParts.scan.getNumber(scan);???
    scan.nextLine();

    Thanks so much!
    Last edited by Freaky Chris; April 6th, 2010 at 04:39 AM. Reason: Added code tags


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Handling Errors / calling Error-Catching Methods

    getNumber("Enter a number: ", scan, 0, 10);

    Where the number must be in the range 0..10

    Chris

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Handling Errors / calling Error-Catching Methods

    This works for the the gist of the getNumber method but the next problem i come across is that the program, when re-prompting for the number, does not continue after the user reenters a correct input.

    it just goes like
    [Enter a number: J
    "J" is not a number
    Enter a number: 1
    .
    .
    .]
    and just stops. what should i do to make the program continue to the other bits?

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Handling Errors / calling Error-Catching Methods

    Can you post the exact code you are using in code tags, and an exact trial run

    Chris

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Handling Errors / calling Error-Catching Methods

    It's ok, i actually just figured it out. Thanks a lot for the quick response! helped a great deal!

Similar Threads

  1. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  2. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM
  3. Replies: 2
    Last Post: November 19th, 2009, 11:55 PM
  4. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM

Tags for this Thread