Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By jim829

Thread: Loop doesn't repeat from where I want it to

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

    Default Loop doesn't repeat from where I want it to

    Hey guys,

    I have just finished a program but I just need help with one thing. First of all here is the code:

    Java Code:
    import java.util.Scanner;
    public class loop2
    {
        public static void main (String[] args)
        {
            Scanner inputReader = new Scanner (System.in);
            char result='y';
         
            do {
                // ’Nytto’-kod:
                int vowels_count = 0;
                int consonents_count = 0;
                int charachters_count= 0;
                System.out.println("Skriv in en text");
                String str = inputReader.nextLine();
                String str2 = str.toLowerCase();
                char[] chr = str2.toCharArray();
                
                for(int i=0;i<chr.length;i++)
                {
                    if(chr[i] == 'a' || chr[i]== 'e' || chr[i] == 'i' || chr[i] == 'o' || chr[i] == 'u')
                        vowels_count++;
                    else if(chr[i] == '-' || chr[i] == '!' || chr[i] == '?' || chr[i] == ',' || chr[i] == '.' || chr[i] == ':' || chr[i] == ';')
                        charachters_count++;
                    else
                        consonents_count++;
                }
                
                System.out.println("Antalet vokaler:"+vowels_count+ " "+"Antalet konsonanter:"+consonents_count+" "+"Antalet interpunktionstecken:"+charachters_count++);
                // 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.nextLine();// H ̈amta anv ̈andarens svar.
                result=repeat.charAt(0);
                if(result=='y')
                {
                    continue;
                }
                else if(result !='y' && result !='n')
                {
                    System.out.println("Du får bara skriva y eller n, försök igen!");
                    result='y';
                }
                else
                {
                    System.out.println ("Klart.");
                    inputReader.close ();
                }
                
                }
            while (result == 'y'); // Observera semikolon!
            
        }
    }
    My problem lies somewhere on row 40-42. The user gets a question if they want to repeat the program by pressing y or n. If they press y the program repeats itself and this works, if they press n it quits and this also works. But when the user input is something else than y or n, they get an error message and then the program repeats itself from the beginning whereas it should repeat itself from the question to input either y or n. What do I have to change on row 40-42?

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

    Default Re: Loop doesn't repeat from where I want it to

    In line 42 you assign the value of 'y'. The next else is skipped and the while continues since result == 'y'. You need some additional logic to handle the case when neither an n or y is provided. Often, prompts of this nature are put inside a loop and only exit on proper responses. Otherwise they repeat until the user enters a valid response.

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

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. Storing A Repeat Loop In An Array
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 07-22-2011, 05:57 AM
  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

Posting Permissions

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