*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....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...System.out.println("Sat: "+time.getFirstEntry()+":"+time.getSecondEntry()+" - "+time.getFirstEntry()+":"+time.getSecondEntry()+"\n")
Thanks to anyone who has read to this line and has a solution to my problem :-)