0

Okay, so the program that I'm trying to figure out how to code (not really fix), I have to use Java to accept continuous input from the user until they enter a period. It then must calculate the total characters that the user input up to the period.

import java.io.*;
class ContinuousInput
{
    public static void main(String[] args) throws IOException
    {
    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader userInput = new BufferedReader (inStream);
    String inputValues;
    int numberValue;
    System.out.println("Welcome to the input calculator!");
    System.out.println("Please input anything you wish: ");

    inputValues = userInput.readLine();

    while (inputValues != null && inputValues.indexOf('.')) {
    inputValues = userInput.readLine();
    }
    numberValue = inputValues.length();
    System.out.println("The total number of characters is " + numberValue + ".");

    System.out.println("Thank you for using the input calculator!");
    }
}

Please don't suggest the use of Scanner, the Java SE Platform we're stuck using is the SDK 1.4.2_19 model and we can't update it. Explanation of empty braces: I thought that if I put in the empty braces that it would allow for continuous input until the period was put in, but clearly that wasn't the case...

Edit: Updated code Current Error: won't end when . is inputted.

Anthony Owen
  • 33
  • 2
  • 7

1 Answers1

3

You have to switch the if/else statement with while.

Sample :

inputValues = userInput.readLine();
while (!".".equals(inputValues) {
   //do your stuff
   //..and after done, read the next line of the user input.
   inputValues = userInput.readLine();
}

Note: Never compare the values of String objects with the == operator. Use the equals() method.

If you just want to test, whether the sentence the user inputs contains a . symbols, you just have to switch from equals() to contains(). It's a built-in method from the java.lang.String class.

Sample:

 while (inputValues != null && !inputValues.contains(".")) {
    //do your stuff
 }
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Thanks for the help! It works, but now I just need to figure out how to debug it. It doesn't seem to want to stop reading if you put the code in one line (IE: this is a sentence.). I also need a model that will read multiple lines and calculate the same thing. – Anthony Owen May 27 '13 at 12:08
  • Sadly, now I get cannot resolve symbol ( while(!".".contains(inputValues)) ) directed at the first quotation, stating that the method contains (java.lang.String). I honestly don't know my Java very well. :/ – Anthony Owen May 27 '13 at 12:15
  • Same error, directed at the .contains now. Java, why you do dis? – Anthony Owen May 27 '13 at 12:18
  • Cannot Resolve Symbol, Method Contains (java.lang.String), Location: class java.lang.String, while (inputValues != null && !inputValues.contains("."), arrow indicating at the .contains(".") area. – Anthony Owen May 27 '13 at 12:22
  • Oh, yes, there is missing closing bracket `)` there. Copy it again and now it should work. – Konstantin Yovkov May 27 '13 at 12:23
  • I already had the closed bracket in the code. I made sure to put it in. – Anthony Owen May 27 '13 at 12:24
  • ok, switch the `inputValues.contains(".")` with `inputValues.indexOf('.') == -1` – Konstantin Yovkov May 27 '13 at 12:29
  • Are you sure it's `inputValues.indexOf('.') == -1` ? – Konstantin Yovkov May 27 '13 at 12:33