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

Thread: I am struggle to control the loop here.(solved)

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default I am struggle to control the loop here.(solved)

    Hi, everyone . my name is sumon and i am from Bangladesh. i am a beginner in Java. i am practicing beginners level programming. I have faced a problem here. if anyone give me the solution then I will be glad.

    I need help. can anyone tell me to control the loop. i don't want to use nested loop for this program. when i will type 'K' then it will get the right guess but when I will type else it will shows try again. but somehow the loop gives two time "try again " message here. kindly give me a solution without using nested loop. the code is here


     
    public class Main {
        public static void main(String[] args) throws java.io.IOException {
            char ch, ignore, answer = 'K';
            System.out.println("Guess the letter from A -Z  Can you guess it? ");
            for (int i = 0; i < 4; ) {
                ch = (char) System.in.read();
                if (ch == answer) {
                    System.out.print("Right  answer");
                    break;
                }
                else
                    System.out.print(" try again ");
                i++;
            }
        }
    }
    Last edited by sumon_bd; December 15th, 2021 at 04:44 AM. Reason: solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,892
    Thanks
    58
    Thanked 2,692 Times in 2,642 Posts

    Default Re: I am struggle to control the loop here.

    a solution without using nested loop.
    Where is the nested loop you want to remove?

    Note: Reading directly from the keyboard can be tricky. It would be better to use the Scanner class by wrapping System.in the the Scanner class's constructor and then using the Scanner class's methods to read.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: I am struggle to control the loop here.

    got the solution from Facebook Bangladesh group here it is

     import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args){
    char ch, answer = 'K';
    Scanner sc = new Scanner(System.in);
    for (  ;  ;){
    System.out.println("Guess the letter from A -Z Can you guess it? ");
    ch = sc.next().charAt(0);
    if (ch == answer) {
    System.out.print("Right answer");
    break;
    }
    else {
    System.out.println("try again \n");
    }
    }
    }
    }
    Last edited by sumon_bd; December 12th, 2021 at 08:38 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,892
    Thanks
    58
    Thanked 2,692 Times in 2,642 Posts

    Default Re: I am struggle to control the loop here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help For Assignment (Problem with Loop Control Structure)
    By Newja in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 22nd, 2017, 08:11 AM
  2. [SOLVED] Need help with Sentinel Control Loop
    By Roku in forum Loops & Control Statements
    Replies: 1
    Last Post: March 21st, 2013, 06:52 AM
  3. Printer control
    By bardd in forum Java Theory & Questions
    Replies: 4
    Last Post: August 31st, 2012, 08:15 AM
  4. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  5. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM