Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Run-time errors

  1. #1
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Run-time errors

    In brief, I am creating a simple program that allows a user to enter the hours (in military time i.e. 24 hour clock)they have worked in their workplace for each day of the week (only shown 'Sat' for this example). I am using netbeans IDE and all seems well with the code until I run it and get run time errors which do not make any sense to me what so ever. Can you help me understand my runtime errors in order for me to solve them?

    Java Code:
    import java.util.Scanner;
    /*Hours Worked Calculator*/
    
    public class Workedhours {
        
        public static void main(String args[]){
            Scanner sc1 = new Scanner(System.in);
            TimeFormat time = new TimeFormat();
        
            System.out.println("Enter the hours worked for the week: \n");
            System.out.println("Sat: "+time.getFirstEntry() +":"+time.getSecondEntry() +" - " +time.getFirstEntry()+":"+time.getSecondEntry());
         
        }
        
        public static class TimeFormat{
            
            Scanner sc2 = new Scanner(System.in);
            int correctHour;
            int correctMinute;
            
            public void setFirstEntry(int setHour){ //this method is to make sure the first entry is no bigger than two digits
            
                if(setHour>0 && setHour<24){
                    
                      correctHour = setHour;        
                }
                else{
                    errorMessage(setHour);
                }
                
                
            }
            
            public void setSecondEntry(int setMins){
                
               if(setMins>0 && setMins<60){
               
                   correctMinute = setMins;
               
               } 
               else{
                   errorMessage(setMins);
               }
            
            }
            
            
            public int getFirstEntry(){
               String string1 = sc2.toString();
                int Hour  = Integer.parseInt(string1);
                setFirstEntry(Hour);
                            
                return correctHour;
            }
            
            public int getSecondEntry(){
            
                int Minute = Integer.parseInt(sc2.toString());
                setSecondEntry(Minute);
                
                return correctMinute;
            }
            
            public void errorMessage(int wrongEntry){
                
                sc2.toString();
            
            }
            
            
        }
        
        }
    runtime errors are as follows:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at Workedhours$TimeFormat.getFirstEntry(Workedhours.j ava:51)
    at Workedhours.main(Workedhours.java:11)
    Java Result: 1

    Hope this request for help wasn't too long-winded but I just wanted to be completely clear in order to get the necessary help

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Run-time errors

    You read these runtime stack traces from the top down until you reach a line of your code. This is a good place to begin looking for the cause of the error. In this case it is line 51. In fact I think it is the line numbered "50" in your post, because the stack trace refers to Integer.parseInt().

    Java Code:
    int Hour  = Integer.parseInt(string1);
    Reading the stack trace upwards, this is causing an error with Integer.parseInt(). The NumberFormatException means that string1 cannot be recognised as an integer number.

    When we look at your code we see that string1 comes from the scanner:

    Java Code:
    String string1 = sc2.toString();
    sc2.toString() is the string "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]" (so the stack trace tells us). toString() produces a diagnostic form of an object: in this case an instance of Scanner. It is not intended to provide you with a string from System.in. There are other methods in the Scanner class that do that and you should use them.

    -----

    Also I wonder whether you should declare a static TimeFormat class. What is this class supposed to do? Whatever it is supposed to do it would do it better as a "top level" class declared in its own file. If you get errors about a "static context", the solution is almost certainly not to create an inner class and just throw the word "static" around until the useful compiler message goes away. As a general rule the only static thing will be the main() method that kicks off the program.

  3. #3
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Run-time errors

    First, thanks for the reply it has helped me understand what I had seen as word spaghetti; I now know it is called 'Stack traces' and am more familiar with how to read them.

    Point 1:

    "toString() produces a diagnostic form of an object: in this case an instance of Scanner. It is not intended to provide you with a string from System.in. There are other methods in the Scanner class that do that and you should use them."

    ...Ok, yeh....I originally thought about using the nextLine() method but decided against it after reading the definition of it and thinking it would mess things up. Looking at the other string methods, this seems like the only sensible one to use.
    I'll let you know if it does what I intend.

    Point 2:

    "Also I wonder whether you should declare a static TimeFormat class. What is this class supposed to do? Whatever it is supposed to do it would do it better as a "top level" class declared in its own file. If you get errors about a "static context", the solution is almost certainly not to create an inner class and just throw the word "static" around until the useful compiler message goes away. As a general rule the only static thing will be the main() method that kicks off the program"

    I 'lol' when I read that because I got that static context error you spoke of and solved it by putting the word static into the method declaration by which all became fine (error message went away). I guess that's a typical solution for a novice.
    The TimeFormat class is intended to be a 'definition class' if that makes sense?
    I'll stick to the general rule you mentioned and I'll also put the TimeFormat class into a file of its own.

  4. #4
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Run-time errors

    try print variable "string1".
    And try this: int Hour = Integer.parseInt(string1.trim());

  5. #5
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Run-time errors

    What exactly does the trim method do? From what I have read it just gets rid of spaces preceding and exceeding the string?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Run-time errors

    Quote Originally Posted by MostinCredible21 View Post
    What exactly does the trim method do? From what I have read it just gets rid of spaces preceding and exceeding the string?
    More precisely, it gets rid of all white space before and after the String. This includes spaces, new line tokens, and the like (anything <= \u0020). The API should explain all this. String#trim()

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Run-time errors

    And post back if you have trouble with using 2 classes and nextLine(). I prefer to talk around a problem rather than post teh codez (as do most here), but that doesn't mean the discussion is ended. Your 'lol' was the intended response, and is appreciated.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Run time errors
    By crabman in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 09:51 PM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  5. Help with Compile time errors
    By bri1547 in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 11:22 PM

Tags for this Thread

Posting Permissions

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