Results 1 to 11 of 11
Thread: buffer problem, how to clean it?
- 09-19-2010, 08:31 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
buffer problem, how to clean it?
Hello there,
I'm new to Java and in the forum. I can program (a bit) in ANSI C and ASP, and this year I'm starting a bachelor in Computer Science in Amsterdam (in dutch hehe).
I have to make a simple program with 4 methods, and I'm having a problem already with the first one :o. I have to convert an integer into HH:MM:SS format. The integer will be the total amount of seconds and I'm getting stucked implementing the typical error control. If you type an 'a' or a '-1' the program will correctly re-ask for the integer, but if you type a correct value after that, it will re-ask again.
In addition, if the input are characters, it will show the error message twice (of course) and I could not think in any other way to do it. Maybe I'm a bit rusty after a couple of years without programming and I don't see how obvious the answer is. The code:
Java Code:import java.util.*; public class Opgave2 { public static void main(String args[]) { boolean incorrectInput = false; int input = 0; /* I tried to implement an error control. It works, but only in the first loop. If a non accepted input is given, for example 'a' or '-1', it will ask for the input again but then even if it is correct it will ask it again. Don't know what to do! Maybe some kind of buffer erase or something. */ do { try { System.out.printf("\nType an amount of seconds: "); Scanner sc = new Scanner(System.in); input = sc.nextInt(); } catch (Exception e) { System.out.println("\nYou can only enter an integer between 0 and 65535"); incorrectInput = true; } if (input < 0 || input > 65535) { System.out.println("\nYou can only enter an integer between 0 and 65535"); incorrectInput = true; } } while (incorrectInput); System.out.println("\nThose seconds in HH:MM:SS format: " + secondsConverter(input)); } public static String secondsConverter(int secs) { int hours = secs / 3600; int minutes = (secs % 3600) / 60; int seconds = (secs - (hours * 3600) - (minutes * 60)); String result = hours + ":" + minutes + ":" + seconds; return result; } }
- 09-19-2010, 08:40 PM #2if you type a correct value after that, it will re-ask again.
Other comments:
Move the definition of the Scanner outside of the loop
Move the test of the value of input immediately following where you successfully get its value instead of outside of the catch block
- 09-19-2010, 11:28 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
Hi there, thx for your answer.
I tried your advices, but I still can't get it working.
The scanner was formerly outside the loop, but when there, it produced an endless loop when typing characters. When I put it in, it solved the problem.
The if inside the catch is not right, cause the process will only enter the catch if no integer is typed (if there is a exception error), and I need to control if it is a natural number.
And about the flag, I'm sorry but I don't know what you mean. A break?
Thanks
- 09-19-2010, 11:50 PM #4
A "flag" is a variable that you set to keep track of the state of something. You use the variable: incorrectInput as a flag to keep track of when you get a good input value.
This is the line that defines the Scanner object:
Scanner sc = new Scanner(System.in);
The if test of the value of input makes sense to be where input is given a value.
- 09-20-2010, 12:02 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
I am sorry man, but I can not find any help in your words.
- 09-20-2010, 12:09 AM #6
What variable controls when the loop exits? When, after a bad input, is the variable set so that loop can exit?
- 09-20-2010, 12:27 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
this one works
I made it work, but I think the solution is too patchy. I would really appreciate it if you could write the code implementing those solutions you said before. Thank you
Java Code:import java.util.*; public class Opgave2 { public static void main(String args[]) { boolean incorrectInput = false; int input = 0, count = 0; do { try { System.out.printf("\nType an amount of seconds: "); Scanner sc = new Scanner(System.in); input = sc.nextInt(); } catch (Exception e) { System.out.println("\nYou can only enter an integer between 0 and 65535"); incorrectInput = true; if (count == 1) count--; } if (input < 0 || input > 65535) { System.out.println("\nYou can only enter an integer between 0 and 65535"); incorrectInput = true; if (count == 1) count--; } if (count > 0 && incorrectInput) incorrectInput = false; count++; } while (incorrectInput); System.out.println("\nThose seconds in HH:MM:SS format: " + secondsConverter(input)); } public static String secondsConverter(int secs) { int hours = secs / 3600; int minutes = (secs % 3600) / 60; int seconds = (secs - (hours * 3600) - (minutes * 60)); String result = hours + ":" + minutes + ":" + seconds; return result; } }
- 09-20-2010, 12:29 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
- 09-20-2010, 12:58 AM #9
The count variable makes the code a mess.
Can you answer these two questions:
1)What variable controls when the loop exits?
2)When, after a bad input, when you get good input, is the variable set so that loop can exit?
- 09-20-2010, 10:30 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
Yes, I totally agree that the counter thing is a mess. Maybe I should rewrite the whole program from another point of view, or forget about the perfect error control, cause we are at the beggining and they don't ask this sort of things yet, but since I already know it, I just wanted to make it perfect.
1)What variable controls when the loop exits?
2)When, after a bad input, when you get good input, is the variable set so that loop can exit?
Java Code:do { try { System.out.printf("\nEnter an amount of seconds: "); Scanner sc = new Scanner(System.in); input = sc.nextInt(); } catch (Exception e) { System.out.println("\nYou can only enter a whole number between 0 and 65535"); incorrectInput = true; } if (input < 0 || input > 65535) { System.out.println("\nYou can only enter a whole number between 0 and 65535"); incorrectInput = true; } if (incorrectInput) incorrectInput = false; } while (incorrectInput);
- 09-20-2010, 01:50 PM #11set the flag to false if it is true, but only after the first loop
One way is to have an else clause with the if that tests if the value is in range. Set the flag there to allow exiting the loop.
Java Code:Scanner sc = new Scanner(System.in); do { try { System.out.printf("\nEnter an amount of seconds: "); input = sc.nextInt(); if (input < 0 || input > 65535) { System.out.println("\nYou can only enter a whole number between 0 and 65535"); incorrectInput = true; // don't exit yet }else { incorrectInput = false; // have good input - exit loop } } catch (Exception e) { System.out.println("\nYou can only enter a whole number between 0 and 65535"); incorrectInput = true; // don't exit yet } } while (incorrectInput);
Similar Threads
-
j2me installation is clean?
By metalmilitia in forum Sun Java Wireless ToolkitReplies: 0Last Post: 10-30-2009, 11:14 AM -
Is there a way to clean up this recursion code?
By SMHouston in forum New To JavaReplies: 2Last Post: 09-30-2009, 02:30 AM -
[REQ] could anybody clean this method up for me?
By harryblue in forum Advanced JavaReplies: 5Last Post: 03-19-2009, 02:37 AM -
[REQ] can sombody clean up this code?
By harryblue in forum New To JavaReplies: 1Last Post: 03-18-2009, 08:40 PM -
clean and Build
By bhanu in forum EclipseReplies: 3Last Post: 07-03-2008, 01:13 PM
Bookmarks