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

Thread: Do while loop skipping method 3 times

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Do while loop skipping method 3 times

    Hello, I started learning java two days ago and I am well on my way on completing my first calculator.

                    do
    		{
    		System.out.println("Indicate the operation: ");
    		input.CheckInputType(symbol[count]); 
    		}
    		while(symbol[count] != '*');



    This code will print "Indicate the operation: " three times before asking for input again IF the symbol isn't *.

    Here is the CheckInputType method in case it is needed:

    EDIT: Apparently, I cannot add two code tags as of now, I'll blend it with the code up there.
    EDIT: That doesn't work either.. I'll just make another post with the second method.

    --- Update ---

    The method:

    package calcPackage;
     
    import java.io.IOException;
     
    public class TypeCheck2 extends TypeCheck
    {
    	@SuppressWarnings("static-access")
    	public boolean CheckInputType(char character)
    	{
    		try
    		{
    			character = (char) System.in.read();
    		} catch (IOException e)
    		{
    			System.out.println("Invalid input!");
    			return false;
    		}
            super.symbol[super.count] = character; //The only way I could think of to pass the input back from the method
    		return true;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    Read the value returned by System.in.read() into a variable and print out its value before casting it to a char.
    You will see what values are returned by the read() method when you press the Enter key.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Do while loop skipping method 3 times

    This has to do with how the character is read in. Once you read the first character, the buffer still has some sort of data in it. You have to clear the buffer once you get your character.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Indicate the operation: 
    -
    45
    Indicate the operation: 
    13
    Indicate the operation: 
    10
    Indicate the operation:

    Why does it take the input three times when I only indicated one symbol? Why do the values change?

    Nvm. How do I clear the buffer?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Do while loop skipping method 3 times

    Show us the code that asks the user to "Indicate the operation:" and accepts/processes the input.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    I believe I have already supplied the necessary information, but.. this may help clarify:

    public static void insertSymbol()
    	{
    		TypeCheck2 input = new TypeCheck2();
     
    		do
    		{
    		System.out.println("Indicate the operation: ");
    		input.CheckInputType(symbol[count]); 
    		}
    		while(symbol[count] != '*');
     
    		if ((symbol[count] == '=') && (count != 0)) 
    		{
    			seeResult = true;
    		}
    	}

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Do while loop skipping method 3 times

    So where do the '45', '13' and '10' come from? What print statement is outputting those values to the terminal?

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Quote Originally Posted by GregBrannon View Post
    So where do the '45', '13' and '10' come from? What print statement is outputting those values to the terminal?
    package calcPackage;
     
    import java.io.IOException;
     
    public class TypeCheck2 extends TypeCheck
    {
    	@SuppressWarnings("static-access")
    	public boolean CheckInputType(char character)
    	{
    		try
    		{
    			int tempCharacter;
    			tempCharacter =  System.in.read();
    			System.out.println(tempCharacter);
    			character = (char)tempCharacter;
     
     
    		} catch (IOException e)
    		{
    			System.out.println("Invalid input!");
    			return false;
    		}
            super.symbol[super.count] = character; //The only way I could think of to pass the input back from the method
    		return true;
    	}
    }

  9. #9
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Do while loop skipping method 3 times

    I would suggest using a Scanner to abstract yourself from the System.in class. It's a bit easier to deal with.

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Quote Originally Posted by Parranoia View Post
    I would suggest using a Scanner to abstract yourself from the System.in class. It's a bit easier to deal with.
    That was my plan, however the Scanner has no sc.readChar();

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Bump. I will post the full program if that is what it takes to find a solution.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    Make a small, simple program that compiles, executes and shows the problem. Copy the full contents of the console window from when you execute the test program and paste it here. Add some comments saying what is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Quote Originally Posted by Norm View Post
    Make a small, simple program that compiles, executes and shows the problem. Copy the full contents of the console window from when you execute the test program and paste it here. Add some comments saying what is wrong.
    package simpleProgram;
     
    import java.io.IOException;
     
    public class Calculator
    {
     
    	public static void main(String[] args)
    	{
     
    		char symbol = 0;
    		do
    		{
    			try
    			{
    				System.out.println("Please indicate the operation:");
    				symbol = (char) System.in.read();
    				//System.in.skip(200); // If I ask it to skip anything else, then it doesn't sh//ow 'Please indi...' three times.
    				//if it is skip(1) and I enter one symbol, it will print 'Please indi...' twice.
    				//so the junk I receive is always two characters aside from what is entered.
    				//How do I make it read only 1 char, and discard anything else regardless of th//e amount of chars entered?
     
    			} catch (IOException e)
    			{
    				System.out.println("Invalid input!");
     
    			}
    		} while (symbol != '*'); //if '((symbol != '*') || (symbol != '/'));' then no matter what I
    // enter,
    		//it's false
    		//if just '(symbol != '*')' then it works as intended, however I need 4 dif. symbols.
    		System.out.println("Operation symbol received successfully!");
     
    	}
     
    }
    [COLOR="Silver"]

    --- Update ---

     Please indicate the operation:
    *
    Operation symbol received successfully!

    Please indicate the operation:
    -
    Please indicate the operation:
    Please indicate the operation:
    Please indicate the operation:
    *
    Operation symbol received successfully!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    What char is read by the read() method? When is the condition in the while() statement true?
    Add a println() statement that prints out what is in the symbol variable so you can see what is read and then understand what the program is doing.


    The read() method is a very low level method that returns what the OS sends to the program. If you are going to use it, you must understand what the OS can send and write code to handle it.

    When I execute the code I get this in the command prompt window:
    D:\JavaDevelopment\Testing\ForumQuestions9>java Calculator1
    Please indicate the operation:
    *
    symbol=*<
    Operation symbol received successfully!

    D:\JavaDevelopment\Testing\ForumQuestions9>java Calculator1
    Please indicate the operation:
    *
    symbol=*<
    Operation symbol received successfully!

    D:\JavaDevelopment\Testing\ForumQuestions9>
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    Quote Originally Posted by Norm View Post
    What char is read by the read() method? When is the condition in the while() statement true?
    Add a println() statement that prints out what is in the symbol variable so you can see what is read and then understand what the program is doing.


    The read() method is a very low level method that returns what the OS sends to the program. If you are going to use it, you must understand what the OS can send and write code to handle it.

    When I execute the code I get this in the command prompt window:
    I added a println just under the
    symbol = (char)System.in.read();

    Please indicate the operation:
    /
    Symbol is: /
    Please indicate the operation:
    Symbol is: 
     
    Please indicate the operation:
    Symbol is: 
     
    Please indicate the operation:
    *
    Symbol is: *
    Operation symbol received successfully!

    Any insights? .. Here's another one where I print out the result before casting to char:
    Please indicate the operation:
    /
    Symbol is: 47
    Please indicate the operation:
    Symbol is: 13
    Please indicate the operation:
    Symbol is: 10
    Please indicate the operation:
    *
    Symbol is: 42
    Operation symbol received successfully!

    If it's a such a low level method, then what would be a better alternative where I can check if the input entered is indeed a char?
    I do realize that what is entered may as well be an int and just get converted into a char.. In any case, I just want it to work as intended. I do not know any other way to handle the input aside from System.in and Scanner.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    Try using some of the Scanner class's methods. You could read into a String, check that its length is 1 and then use charAt(0) == 'X' where X is the character to test for or the equals("X") method to test what is in the String.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    So it's alright to dismiss the System.in method as unneeded or unreliable?

    Aside from that.. The renewed version still does funky stuff.

    package simpleProgram;
     
    import java.io.IOException;
    import java.util.Scanner;
     
    public class Calculator
    {
     
     
    	public static void main(String[] args) //throws IOException
    	{
     
    		String symbol = null;
    		Scanner sc = new Scanner(System.in);
    		do
    		{
    			System.out.println("Please indicate the operation:");
    			symbol =  sc.next();
    			System.out.println("Symbol is: " + symbol);
    		} while ((symbol.charAt(0) != '*') && symbol.length() != 1); 
    		System.out.println("Operation symbol received successfully!");
     
    	}
     
    }
    Please indicate the operation:
    ///////////
    Symbol is: ///////////
    Operation symbol received successfully!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    I wouldn't say unreliable. It does what it is supposed to do and requires some knowledge to be able to use.
    does funky stuff.
    What it the problem with the code now? You should add comments describing what is wrong:
    The program printed out this:
    THE PROGRAMS OUTPITT
    but I wanted this:
    The output


    Full conditions that use AND operators to connect sub condition require that ALL of the sub conditions be true for the full condition to return true.
    With the OR operator ONLY ONE sub condition needs to be true for the full condition to return true.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    TheoMuffin (November 25th, 2013)

  20. #19
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    I updated the code above. I made a mistake, however it still doesn't do what I wanted.

    The program printed out this:
    Please indicate the operation:
    *****
    Symbol is: *****
    Operation symbol received successfully!
    However I want it to print stuff like this:
    Please indicate the operation:
    ///////////
    Symbol is: ///////////
    Please indicate the operation:
    *****
    Symbol is: *****
    Please indicate the operation:
    /
    Symbol is: /
    Please indicate the operation:
    *
    Symbol is *
    Operation symbol received successfully!

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    Did you see what I wrote in post#18 about a condition like what is used in the while statement?

    When do you want the do{} while to loop

    and when do you want it to exit the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    TheoMuffin (November 25th, 2013)

  23. #21
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    I want the do {} while to loop until I get the input I desire. In this case, a symbol '*' which is of length 1.

    I want it to exit the loop when I receive the above-mentioned symbol.

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    OK, what is the condition the code needs to continue looping?

    What condition will return true so the loop will continue?

    What condition will return false so the loop will exit?


    Write a small test program that has a String and use a println() statement to print out the value of different conditions. Have many different println statements with different combinations of the condition. The print out will show which are true and which are false:
    String symbol = "???";  // change this for other tests
    System.out.println("1st="+((symbol.charAt(0) != '*') && symbol.length() != 1));

    Change "1st" to "2nd", "3rd" etc for each condition tested.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    TheoMuffin (November 25th, 2013)

  26. #23
    Junior Member
    Join Date
    Nov 2013
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop skipping method 3 times

    You repeatedly tell me to look at the conditions, hence I am blind to my mistake and it is something incredibly silly and obvious.
    I realized that my human logic did not work here.

    I want the loop to exit when the symbol is * AND the string length is 1, however I noticed that it will keep looping only while both of the conditions are not true. I want it to keep looping while........... either of the statements are not true. Yes, that should do the trick.. Incredibly silly of me but it takes me a while. It's kind of like thinking backwards right now.

    Thank you for your patience, the program works fine now.

  27. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do while loop skipping method 3 times

    Glad you you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    TheoMuffin (November 25th, 2013)

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. Replies: 4
    Last Post: September 23rd, 2013, 08:04 PM
  3. Skipping parts of if loop (Coin toss Program)
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2012, 03:52 PM
  4. [SOLVED] My program skipping a for loop
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2012, 09:05 AM
  5. need help prog skipping method
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2010, 08:57 AM