0

I'm trying to create a program that receives input characters from the user and converts the lowercase characters to uppercase and vice versa. Every time a conversion is made, the number of changes increments and when '.' is inputted, the program stops asking for an input. This is what I have down so far:

import java.io.*;
class Example5
{
   public static void main(String args[]) throws IOException
   {
      InputStreamReader inStream = new InputStreamReader (System.in);
      BufferedReader stdin = new BufferedReader (inStream);
      char input = '\0';
      int counter = 0;

      while(!(input == '.'))
      {
         System.out.print("Input a character. Input will continue until you enter a period : ");
         input = (char)stdin.read();

         if((int)input > 96 & (int)input < 123)
         {
            char upperInput = Character.toUpperCase(input);
            System.out.println(upperInput);
            counter++;
         }
         else if((int)input > 64 & (int)input < 91)
         {
            char lowerInput = Character.toLowerCase(input);
            System.out.println(lowerInput);
            counter++;
         } 
      }

      System.out.println("The number of changes are : " + counter);
   }
}

The conversion and the counter works fine but for some reason, after every input, the line "Input a character. Input will continue until you enter a period : " repeats multiple times after every input. Any solution to this problem? What mistake did I make?

Thanks in advance :)

  • you just have to move `System.out.print("Input a character. Input will continue until you enter a period : ");` above the `while` loop. – yate May 19 '14 at 17:02
  • user3580294 - thank you! didn't know about this! – user3529827 May 19 '14 at 17:04
  • yate - yup, I changed it to this way. But I was also curious about why the line would be repeated multiple times. thanks alot! – user3529827 May 19 '14 at 17:05
  • 1
    @user3529827 No problem! It's something that tends to take everyone by surprise at some point, in my experience. Whenever I use `Scanner` I tend to only use `nextLine()` just for this reason. It's a bit more hassle to parse the resulting `String`, but it's less of a hassle than dealing with these kinds of shenanigans. – awksp May 19 '14 at 17:06
  • you can use do-while loop as your print statement is inside the while loop! – Java Nerd May 19 '14 at 17:10

3 Answers3

3

Your print statement is within your while loop. This causes the program to print every time the loop starts.

The loop doesn't wait for more input, it will loop regardless of whether there is new input or not.

To fix it, either take the print statement out of the loop if you only want that statement to be printed once at the beginning of the program execution or change the loop condition so that it only re-executes when new input is given.

I hope I have been clear and helpful.

rpassza
  • 226
  • 1
  • 2
  • 8
0

Put it outside the while loop

As it's in the loop, each time the character is read, its getting printed.

System.out.print("Input a character. Input will continue until you enter a period : ");

      while(!(input == '.'))
mohamedrias
  • 18,326
  • 2
  • 38
  • 47
0

You can do something like:

InputStreamReader inStream = new InputStreamReader (System.in);
      BufferedReader stdin = new BufferedReader (inStream);
      char input = '\0';
      int counter = 0;
      System.out.print("Input a character. Input will continue until you enter a period : ");
      do
      {

         input = (char)stdin.read();
         System.out.print("Input a character. Input will continue until you enter a period : ");
         if((int)input > 96 & (int)input < 123)
         {
            char upperInput = Character.toUpperCase(input);
            System.out.println(upperInput);
            counter++;
         }
         else if((int)input > 64 & (int)input < 91)
         {
            char lowerInput = Character.toLowerCase(input);
            System.out.println(lowerInput);
            counter++;
         } 
      }while(!(input == '.'));

      System.out.println("The number of changes are : " + counter);
   }
Java Nerd
  • 958
  • 3
  • 19
  • 51