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 3 of 3

Thread: Scanner overriding output stream

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Scanner overriding output stream

    *DO NOT BE DISCOURAGED TO READ DUE TO LENGTH, IT IS QUICKER TO READ THAN YOU THINK*

    My issue is that I cannot get my output stream to look and work the way I intend.

    I am creating a program for users to enter the hours they have worked at their workplace, e.g. 8:00 - 16:30 (In the following example these times will be used and the day Saturday (sat) will be the only day shown).

    What I wish to see and happen, in sequence, in my output window (I'm using netbeans IDE) is shown below in the following numbered steps (step (1) will be the first prompt the user will see));

    **INTENDED OUTPUT**
    STEPS 1-5 ARE DONE IN A SINGLE RUN OF THE PROGRAM
    (1)
    ' Enter the hours worked for the week:
    Sat: (this space 'waits' for user to enter hour) '

    Now, when user enters the hour ,8, I want the program to acknowledge this and output the following stream;

    (2)
    ' Enter the hours worked for the week:
    Sat: 8 : (this space 'waits' for user to enter minutes) '

    (3)
    ' Enter the hours worked for the week:
    Sat: 8 : 00 - (this space 'waits' for user to enter hour) '

    (4)
    ' Enter the hours worked for the week:
    Sat: 8 : 00 - 16 : (this space 'waits' for minutes to be entered by user) '

    (5)
    ' Enter the hours worked for the week:
    Sat: 8 : 00 - 16 : 30

    As can be seen the colons appear automatically after the hour has been entered and the dash appears automatically after the minutes from the start time has been entered..................... how could i code to do something like that? My feeling is loops will be needed as a solution but I cannot visualise in mind how do it.....?

    ....I am getting the following sequence of things happening instead.

    **CURRENT OUTPUT**
    STEPS 1-6 ARE DONE IN A SINGLE RUN OF THE PROGRAM
    (1)
    ' Enter the hours worked for the week:
    (this space 'waits' for user to input hour)'

    (2)
    ' Enter the hours worked for the week:
    8
    (this space 'waits' for user to input minutes)'

    (3)
    ' Enter the hours worked for the week:
    8
    00
    (this space 'waits' for user to input hour)'

    (4)
    ' Enter the hours worked for the week:
    8
    00
    16
    (this space 'waits' for user to input minutes)'

    (5)
    ' Enter the hours worked for the week:
    8
    00
    16
    30

    (6)
    ' Enter the hours worked for the week:
    8
    00
    16
    30
    Sat: 8 : 00 - 16 : 30

    The last line of step (6) automatically shows after entering 30 minutes and pressing the return key.

    The two classes of code below is how I am getting the **CURRENT OUTPUT**, but how would I amend it to get the **INTENDED OUTPUT**???

    import java.util.Scanner;
    /*Hours Worked Calculator*/
     
    public class Workedhours {
     
        public static void main(String args[]){
     
            TimeFormat time = new TimeFormat();
            Scanner sc1 = new Scanner(System.in);
     
            System.out.println("Enter the hours worked for the week: ");
            System.out.println("Sat: "+time.getFirstEntry()+":"+time.getSecondEntry()+" - "+time.getFirstEntry()+":"+time.getSecondEntry()+"\n");
     
        }
     
      }

    import java.util.Scanner;
     
    public 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 within the restrictions for military time
                correctHour = ((setHour>=0 && setHour<24) ? setHour:00);
                }
     
            public void setSecondEntry(int setMins){ //this method is to make sure the second entry is within the restrictions for military time
     
               if(setMins>0 && setMins<60){
     
                   correctMinute = setMins;
     
               } 
               else{
                   errorMessage(setMins);
               }
     
            }
     
             public int getFirstEntry(){
               String string1 = sc2.next();
                int Hour  = Integer.parseInt(string1);
                setFirstEntry(Hour);
     
                return correctHour;
            }
     
            public int getSecondEntry(){
     
                int Minute = Integer.parseInt(sc2.next()); //this is the equivalent of lines 36 and 37 in one line.
                setSecondEntry(Minute);
     
                return correctMinute;
            }
     
     
            public void errorMessage(int wrongEntry){
     
                 //I am hoping this simply resets whatever is entered by user with a blinking cursor ready to read in text again
     
            }
     
    }

    Line....
    System.out.println("Sat: "+time.getFirstEntry()+":"+time.getSecondEntry()+" - "+time.getFirstEntry()+":"+time.getSecondEntry()+"\n")
    is responsible for the title of this post, because it seems to me that the Scanner in the get Methods is overriding/stopping 'sat' from showing...

    Thanks to anyone who has read to this line and has a solution to my problem :-)
    Last edited by MostinCredible21; October 4th, 2011 at 05:09 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Scanner overriding output stream

    Input from the console doesn't get put into the input stream buffer until the console flushes (i.e. the enter key gets pressed). Scanner.next() is a blocking method and won't continue on until it reaches the end of the stream or reads something in the stream.

    Unfortunately there's not a portable way to do what you want and thus it can't be done in pure Java code.

    A suggestion is to use a GUI to do this, you have much greater control over user input/output in a GUI than you do in a console application.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    MostinCredible21 (October 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Scanner overriding output stream

    Thanks a lot of that information

Similar Threads

  1. could not create audio stream from input stream
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 2nd, 2011, 02:08 AM
  2. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  3. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  4. Overloading vs Overriding
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:53 AM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM