0

I am just finishing my program, but I still have one problem left that I can't seem to find an answer too. I have been looking through already asked questions but I couldn't find something that specifically answers my question in this case. This is a program that lets the user input a string and then it counts how many vowels and consonants etc. And after this the user gets an option to repeat the program and input a new string if he/she press y, the program quits if he/she press n etc. The only thing that is not working is if the user presses y to repeat the program, it then prints out that there are 0 vowels and consonants etc. I know that it is something in the beginning of my code where I have int consonant_count=0 for example, I just can't figure out what to move and where to move it. Ps. this shouldn't be flagged as a duplicate since I didn't know that nextLine was the problem. Here is the 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.next();// 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!

    }
}
mackanmorre
  • 135
  • 1
  • 12
  • I should also add that the same thing happens when the user input is something else than y or n, then they should get a message telling them to try again and then the program should repeat itself. – mackanmorre Apr 13 '17 at 17:14
  • "aeiou".indexOf(z) < 0 better to test for vowel, idem for the second test – azro Apr 13 '17 at 17:15

1 Answers1

0

You should use the nextLine() when reading input from the user, this grabs everything including the end of line character '\n' which is what gets left over after your next() call and then nextLine() grabs the '\n' which gives you the counts of 0, 0 for vowels and consonents

    Scanner inputReader = new Scanner (System.in);
    char result='y';
    while(result == 'y')
    {
        // ’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++);
        //wrap your play again logic in another do/while where you
        // ask for y or n until they enter either one
        do {
            System.out.println ("För att upprepa: Skriv y");
            System.out.println ("För att avsluta: Skriv n");
            String repeat=inputReader.nextLine();//read the entire next line <----
            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!");
            }
            else
            {
                System.out.println ("Klart.");
                inputReader.close ();
            }
        } while (result !='y' && result !='n');
    }
RAZ_Muh_Taz
  • 3,964
  • 1
  • 10
  • 22
  • Thank you! This works perfectly when i press y. But when i press something else than y or n, it repeats the program from when the user should input a string, but it should repeat itself and ask the user to input y or n. Do you know what I can change there? – mackanmorre Apr 13 '17 at 17:23
  • added another do/while to help with asking until the user enters something valid @mackanmorre – RAZ_Muh_Taz Apr 13 '17 at 17:32
  • Thank you so much! – mackanmorre Apr 15 '17 at 17:31