-1

When I am inputing a string that is not matching to the condition in the while loop, the loop continues

I was asked to write a function that finds the number of vowels in a String, and a main program which is sending an input to the function and the function returns the number of vowels in the string. Then, in the main program prints the string with the biggest number of vowels.

I take input while in one input the string is not empty ("") and when I press enter to input an empty string the while loop continues and does not print the string with the biggest number of vowels as it should.

Code:

import java.util.*;
class ex_5
{
      public static int checkvowels (String sentence)
      {
          int countvowels=0;
          int i;
          for (i=0;i<sentence.length();i++)
          {
              if ((sentence.charAt(i)=='a')||(sentence.charAt(i)=='e')||(sentence.charAt(i)=='i')||(sentence.charAt(i)=='o')
              ||(sentence.charAt(i)=='u')|| (sentence.charAt(i)=='A')||(sentence.charAt(i)=='E')||(sentence.charAt(i)=='I')
              ||(sentence.charAt(i)=='O')||(sentence.charAt(i)=='U'))
              {
              countvowels++;
            }
        }
            return countvowels;

    }
   public static Scanner reader= new Scanner(System.in);
   public static void main(String[]args)
   {
     int max=0;
     String maxvowels="";
     System.out.println("Insert a sentence");
     String sentence=reader.next();
     while (!sentence.equals(""))
     {
         if (checkvowels(sentence)>max)
            {
                max=checkvowels(sentence);
                maxvowels=sentence;
            }
          System.out.println("Insert a sentence");
           sentence=reader.next();
        } 
        System.out.println(maxvowels);
    }
}

I am expecting the code to exit the while when inputting an empty string and to do the next command. Thank you for your help!

alxlives
  • 4,505
  • 4
  • 22
  • 45
  • What next command? The while loop is the last thing in your main method. – azurefrog Sep 10 '19 at 14:38
  • 2
    By default `next()` returns next token which can't be empty string since default delimiter is set to be *one or more whitespaces*. Maybe use `nextLine` instead to consume entire line, but be careful about problem described at [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo Sep 10 '19 at 14:42
  • Also `next()` returns *single* token so either `sentence` is not best name for that variable, or if you want to read entire line use `nextLine` instead. – Pshemo Sep 10 '19 at 14:43
  • I close the while statement before System.out.println(maxvowels); The two last brackets are to close the class and public static – Python_Programmer Sep 10 '19 at 14:44

2 Answers2

0

You need to replace reader.next() with reader.nextLine()

next() : scans only up to the first space character it encounters.
while
nextLine() : reads everything up to the '\n' character (each time you press enter)

A more detailed question about the difference between next() and nextLine() is already answered in the following link:

What's the difference between next() and nextLine() methods from Scanner class?

clumsypr
  • 31
  • 3
0

You must replace reader.next() with reader.nextLine(). reader.next() does not take the entire sentence, but rather just one word. I ran the code below and it worked perfectly when that change was made.

import java.util.Scanner;
public class Main {

    public static int checkvowels (String sentence)
    {
        int countvowels=0;
        int i;
        for (i=0;i<sentence.length();i++)
        {
            if ((sentence.charAt(i)=='a')||(sentence.charAt(i)=='e')||(sentence.charAt(i)=='i')||(sentence.charAt(i)=='o')
                    ||(sentence.charAt(i)=='u')|| (sentence.charAt(i)=='A')||(sentence.charAt(i)=='E')||(sentence.charAt(i)=='I')
                    ||(sentence.charAt(i)=='O')||(sentence.charAt(i)=='U'))
            {
                countvowels++;
            }
        }
        return countvowels;

    }
    public static Scanner reader= new Scanner(System.in);
    public static void main(String[]args)
    {
        int max=0;
        String maxvowels="";
        System.out.println("Insert a sentence");
        String sentence=reader.nextLine();
        while (!sentence.equals(""))
        {
            if (checkvowels(sentence)>max)
            {
                max=checkvowels(sentence);
                maxvowels=sentence;
            }
            System.out.println("Insert a sentence");
            sentence=reader.nextLine();
        }
        System.out.println(maxvowels);
    }
}
Matthew Gaiser
  • 3,740
  • 1
  • 10
  • 24