Results 1 to 17 of 17
Like Tree2Likes
  • 2 Post By jim829

Thread: Repeat loop with string input

  1. #1
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Repeat loop with string input

    Hey, I'm pretty new to java and I have a quick question. This is a basic program where I want the loop to be repeated when the user input is y, and if the user input is n the program quits. I have done this so far, but something is not right. If you have any suggestions please tell me.

    import java.util.Scanner;
    public class loop
    {
    public static void main (String[] args)
    {
    Scanner inputReader = new Scanner (System.in);
    String result="y";// while-satsen exekveras minst en g ̊ang
    // 1.: kod som ska exekveras.
    // oavsett v ̈ardet i ’repeat’.
    do {
    // ’Nytto’-kod:
    System.out.println ("Exekvera kod i while-satsen.");
    // Kod f ̈or hantering av repetition
    System.out.println ("För att upprepa: Skriv y");
    System.out.println ("För att avsluta: Skriv n");
    string repeat=inputreader.next();// H ̈amta anv ̈andarens svar.
    result=repeat.charAt(0);
    }
    while (result = "y"); // Observera semikolon!
    System.out.println ("Klart.");
    inputReader.close ();
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Repeat loop with string input

    something is not right
    Please copy the full text of the error messages and paste it here. It has important info about the error.

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

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

    to get highlighting and preserve formatting.
    Last edited by Norm; 04-11-2017 at 04:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Repeat loop with string input

    Do boolean comparisons with == instead of =. Having said that, do String comparisons with equals() or equalsIgnoreCase().
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Repeat loop with string input

    The problem probably lies here.

    Java Code:
    result=repeat.charAt(0);
                }
            while (result = "y"); // Observera semikolon!
    First, use == and not = to compare primitives.
    Second, use 'y' and not "y" for a char.

    So use
    while(result == 'y');

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Repeat loop with string input

    And it's Norm, Surfman, and Jim with a Win, Place, and Show.

    Regards,
    Jim
    SurfMan and Norm like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Repeat loop with string input

    You guys only pointed out 1 of the 3 errors. The OP needs to know how to find the compiler's error messages and post them so that future posts can be handled more easily.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Repeat loop with string input

    Quote Originally Posted by Norm View Post
    You guys only pointed out 1 of the 3 errors. The OP needs to know how to find the compiler's error messages and post them so that future posts can be handled more easily.
    I have an excuse: I have not made the effort to dig through the unformatted mess, so I went for the low hanging fruit. Did you already mention the string instead of String?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    Sorry guys, I can't edit it anymore, so I will just post here.

    Java Code:
    import java.util.Scanner;
    public class loop
    {
        public static void main (String[] args)
        {
            Scanner inputReader = new Scanner (System.in);
            String result='y';// while-satsen exekveras minst en g ̊ang
            // 1.: kod som ska exekveras.
            // oavsett v ̈ardet i ’repeat’.
            do {
                // ’Nytto’-kod:
                System.out.println ("Exekvera kod i while-satsen.");
                // Kod f ̈or hantering av repetition
                System.out.println ("För att upprepa: Skriv y");
                System.out.println ("För att avsluta: Skriv n");
                String repeat=inputReader.next();// H ̈amta anv ̈andarens svar.
                result=repeat.charAt(0);
                }
            while (result == 'y'); // Observera semikolon!
            System.out.println ("Klart.");
            inputReader.close ();
        }
    }
    And here is the error message:

    loop.java:7: error: incompatible types: char cannot be converted to String
    String result='y';// while-satsen exekveras minst en g ̊ang
    ^
    loop.java:17: error: incompatible types: char cannot be converted to String
    result=repeat.charAt(0);
    ^
    loop.java:19: error: incomparable types: String and char
    while (result == 'y'); // Observera semikolon!
    ^
    3 errors

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Repeat loop with string input

    incompatible types: char cannot be converted to String
    String result='y';// while-satsen exekveras minst en g ̊ang
    Strings and chars are different
    Either change the definition of result to a char
    or change the literal ('y') to a String "y"
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    I did. But i still get this error message.

    Java Code:
    import java.util.Scanner;
    public class loop
    {
        public static void main (String[] args)
        {
            Scanner inputReader = new Scanner (System.in);
            String result="y";// while-satsen exekveras minst en g ̊ang
            // 1.: kod som ska exekveras.
            // oavsett v ̈ardet i ’repeat’.
            do {
                // ’Nytto’-kod:
                System.out.println ("Exekvera kod i while-satsen.");
                // Kod f ̈or hantering av repetition
                System.out.println ("För att upprepa: Skriv y");
                System.out.println ("För att avsluta: Skriv n");
                String repeat=inputReader.next();// H ̈amta anv ̈andarens svar.
                result=repeat.charAt(0);
            }
            while (result == "y"); // Observera semikolon!
            System.out.println ("Klart.");
            inputReader.close ();
        }
    }
    error message:
    loop.java:17: error: incompatible types: char cannot be converted to String
    result=repeat.charAt(0);

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Repeat loop with string input

    In your previous post, result was still declared a String in line 7.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    Yeah everything is string in my previous post. As norm said, I either change it to char or string. But since everything is string now I don't get why it won't work.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Repeat loop with string input

    loop.java:17: error: incompatible types: char cannot be converted to String
    result=repeat.charAt(0);
    The charAt() method returns a char, not a String. result is a String.

    If you are going to work with char values, then all the variables and literals should be char, not String.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    I changed the code like this. So I only use the string. But the code repeats itself all the time. I only want it to execute the "do" once and then the user decides if it gets repeated or not.

    Java Code:
    import java.util.Scanner;
    public class loop
    {
        public static void main (String[] args)
        {
            Scanner inputReader = new Scanner (System.in);
            String result="y";// while-satsen exekveras minst en g ̊ang
            // 1.: kod som ska exekveras.
            // oavsett v ̈ardet i ’repeat’.
            do {
                // ’Nytto’-kod:
                System.out.println ("Exekvera kod i while-satsen.");
                // Kod f ̈or hantering av repetition
                System.out.println ("För att upprepa: Skriv y");
                System.out.println ("För att avsluta: Skriv n");
                String result=inputReader.next();// H ̈amta anv ̈andarens svar.
                
            }
            while (result == "y"); // Observera semikolon!
            System.out.println ("Klart.");
            inputReader.close ();
        }
    }
    Im guessing I have to change the value String result="y" to something else, but I'm not sure what.

    loop.java:16: error: variable result is already defined in method main(String[])
    String result=inputReader.next();// H ̈amta anv ̈andarens svar.
    ^

  15. #15
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    And if change everything to char like this:

    Java Code:
    import java.util.Scanner;
    public class loop
    {
        public static void main (String[] args)
        {
            Scanner inputReader = new Scanner (System.in);
            char result='y';// while-satsen exekveras minst en g ̊ang
            // 1.: kod som ska exekveras.
            // oavsett v ̈ardet i ’repeat’.
            do {
                // ’Nytto’-kod:
                System.out.println ("Exekvera kod i while-satsen.");
                // Kod f ̈or hantering av repetition
                System.out.println ("För att upprepa: Skriv y");
                System.out.println ("För att avsluta: Skriv n");
                char repeat=inputReader.next();// H ̈amta anv ̈andarens svar.
                result=repeat.charAt(0);
            }
            while (result == 'y'); // Observera semikolon!
            System.out.println ("Klart.");
            inputReader.close ();
        }
    }
    error message:

    loop.java:16: error: incompatible types: String cannot be converted to char
    char repeat=inputReader.next();// H ̈amta anv ̈andarens svar.
    ^
    loop.java:17: error: char cannot be dereferenced
    result=repeat.charAt(0);
    ^
    2 errors

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Repeat loop with string input

    Sorry, I didn't mean ALL the variables. I meant Just the ones that were working with chars.
    The Scanner method returns a String, so define the variable that gets the data from next as a String.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    mackanmorre is offline Member
    Join Date
    Apr 2017
    Posts
    12
    Rep Power
    0

    Default Re: Repeat loop with string input

    Thank you so much! It works now! I have another question regarding this program, but I will save that one for tomorrow (I am going to try to solve it by myself tonight first)

Similar Threads

  1. Do While loop repeat issue
    By westcoastrendkill in forum New To Java
    Replies: 13
    Last Post: 12-13-2016, 04:23 PM
  2. While loop repeat
    By Nb4bi9 in forum New To Java
    Replies: 3
    Last Post: 12-11-2012, 12:16 AM
  3. repeat if statement in loop
    By eng_hyzoom in forum New To Java
    Replies: 5
    Last Post: 03-05-2012, 12:34 PM
  4. Replies: 8
    Last Post: 11-18-2011, 06:38 PM
  5. Repeat while answer yes - do while loop
    By AJ2009 in forum New To Java
    Replies: 1
    Last Post: 01-10-2009, 06:19 PM

Tags for this Thread

Posting Permissions

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