Results 1 to 11 of 11
  1. #1
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Question 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;
    	}
    }
    And of course, Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    if you type a correct value after that, it will re-ask again.
    When you get a correct input, you can set the flag to let the code exit the loop.

    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

  3. #3
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    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.

  5. #5
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    I am sorry man, but I can not find any help in your words.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    What variable controls when the loop exits? When, after a bad input, is the variable set so that loop can exit?

  7. #7
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default 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;
    	}
    }

  8. #8
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What variable controls when the loop exits? When, after a bad input, is the variable set so that loop can exit?
    sorry I read that after posting my solution hehe

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    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?

  10. #10
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    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?
    incorrectInput

    2)When, after a bad input, when you get good input, is the variable set so that loop can exit?
    That's the whole problem, I have to set the flag to false if it is true, but only after the first loop, so something like the following isn't working... I don't know where to put it. That's why I used the counter for the loop.

    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);

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    set the flag to false if it is true, but only after the first loop
    After you get a good number and have verified that it is in range, set the flag to allow the code to exit the 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

  1. j2me installation is clean?
    By metalmilitia in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-30-2009, 12:14 PM
  2. Is there a way to clean up this recursion code?
    By SMHouston in forum New To Java
    Replies: 2
    Last Post: 09-30-2009, 03:30 AM
  3. [REQ] could anybody clean this method up for me?
    By harryblue in forum Advanced Java
    Replies: 5
    Last Post: 03-19-2009, 03:37 AM
  4. [REQ] can sombody clean up this code?
    By harryblue in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 09:40 PM
  5. clean and Build
    By bhanu in forum Eclipse
    Replies: 3
    Last Post: 07-03-2008, 02:13 PM

Posting Permissions

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