0

So here it is Pschemo my entire code, thanks a lot, there is no problem no errors and everything is just fine. But just one thing to asks at the first time when I enter all the elements of the array and I type stop, it shifts one line as I wrote System.out.println(), and then I have to press enter once again, then it show my designed Thank You message and the rest of Output. Why this happens?

Any Clue?

  import java.util.ArrayList;
 import java.util.Scanner;
 import java.util.concurrent.TimeUnit;

     class End_Start_Vowels
      {
public static void main() throws InterruptedException
{
  String fs="yes";
  while(fs.equalsIgnoreCase("yes"))
  {

      Scanner in=new Scanner(System.in);
      ArrayList <String> a= new ArrayList<String>();
      String b; int c=0;
     char d;
      int total=0;
     int v=0;
     int o=0;
     int w=0;
     int e=0;
     int l=0;
      System.out.println("start tping below, type stop or end to stop.");
      do
      {
        b=in.nextLine();
        a.add(b);
        c++;
      }
      while(!b.equalsIgnoreCase("stop")&&!b.equalsIgnoreCase("end"));

     if(c<2)
      a.clear();

       int z=a.size();
       if(c>=2)
       a.remove(z-1);
      System.out.println();

        if(c<2)
         System.out.println("No word are typed. Please type continue  to restart it");
        String s1=in.nextLine();

       if(c<2)
         {   if(s1.equalsIgnoreCase("continue")){
        System.out.println("start tping below, type stop or end to stop.");
         do
        {
        b=in.nextLine();
        a.add(b);
        c++;
           }
        while(!b.equalsIgnoreCase("stop")&&!b.equalsIgnoreCase("end"));

        if(c<2)
        a.clear();

        z=a.size();
       if(c>=2)
       a.remove(z-1);
       System.out.println();
      for(int j=0;j<a.size();j++)
      {
        String s=a.get(j);

        for(int i=0;i<s.length();i++)
           {
             d=s.charAt(i);


            if(d=='a'||d=='A')
            {v++; total++;}
              if(d=='E'||d=='e')
             { o++; total++;}
              if(d=='i'||d=='I')
             {w++; total++;}
            if(d=='O'||d=='o')
            { e++; total++;}
            if(d=='u'||d=='U')
            { l++; total++;}
           }
      }
      if(c>=2)
       {
            System.out.println("Thanks for using!");
          TimeUnit.SECONDS.sleep(1);

         System.out.println("Showing you the frequencies of each vowels.");
        TimeUnit.SECONDS.sleep(1);
        System.out.println();
        System.out.println("Frequency of vowel a is ="+v);
        System.out.println("Frequency of vowel e is ="+o);
        System.out.println("Frequency of vowel i is ="+w);
        System.out.println("Frequency of vowel o is ="+e);
        System.out.println("Frequency of vowel u is ="+l);
         System.out.println();
        System.out.println("Total number of vowels present is ="+total);
     }
    }

}
else
{ 
      for(int j=0;j<a.size();j++)
      {
        String s=a.get(j);

        for(int i=0;i<s.length();i++)
           {
             d=s.charAt(i);


            if(d=='a'||d=='A')
            {v++; total++;}
              if(d=='E'||d=='e')
             { o++; total++;}
              if(d=='i'||d=='I')
             {w++; total++;}
            if(d=='O'||d=='o')
            { e++; total++;}
            if(d=='u'||d=='U')
            { l++; total++;}
           }
      }
      if(c>=2)
       {
            System.out.println("Thanks for using!");
          TimeUnit.SECONDS.sleep(1);

         System.out.println("Showing you the frequencies of each vowels.");
        TimeUnit.SECONDS.sleep(1);
        System.out.println();
        System.out.println("Frequency of vowel a is ="+v);
        System.out.println("Frequency of vowel e is ="+o);
        System.out.println("Frequency of vowel i is ="+w);
        System.out.println("Frequency of vowel o is ="+e);
        System.out.println("Frequency of vowel u is ="+l);
         System.out.println();
        System.out.println("Total number of vowels present is ="+total);
     }
    }
       System.out.println();
   TimeUnit.SECONDS.sleep(1);
   System.out.println("Do you want to use it again, type yes to continue.");
   fs=in.nextLine();
   System.out.println();
 }
}
}
Gourav
  • 7
  • 5
  • Read the question that was marked as a duplicate. After doing that try this: `while(!b.toLowerCase().contains("stop"));` – Robin Topper May 04 '17 at 10:45
  • Take a look at duplicate question. In short use `!b.toLowerCase().equals("stop")` or simpler `!b.equalsIgnoreCase("stop")`. If you will use Scanner more one of your next duplicates will most likely be: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo. This can also be helpful: http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner, http://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-using-scanner. – Pshemo May 04 '17 at 10:46
  • Thanks, basically I was knowing that next duplicates, haha. Well I am not that bit backward in java. Anyways thanks – Gourav May 04 '17 at 10:52
  • Pshemo you said, "if you will use scanner more", did you mean scanner is not that good? – Gourav May 04 '17 at 11:00
  • Scanner is very good class for simple console-based apps. But sooner or later you will move to other ways to interact with user like with GUI (swing) or via browser (web applications), so Scanner will not be that useful. You will also need to read text stored in specific format like JSON or XML in which case you will need proper parser rather than Scanner. – Pshemo May 04 '17 at 16:02
  • And yes thanks, my code is complete have a thanks Pschemo really very much, not possible without you. Have a look, i just edited my question with my new completed code, as you closed my question. – Gourav May 04 '17 at 18:54

0 Answers0