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.

3
  • That code copied from your dev IDE with those > characters ? Commented May 27, 2013 at 11:58
  • You probably want to use loops (while/for) Commented May 27, 2013 at 11:58
  • See this answer for a comparable solution: stackoverflow.com/a/16441949/714965 Commented May 27, 2013 at 11:59

1 Answer 1

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
 }
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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. :/
Same error, directed at the .contains now. Java, why you do dis?
Cannot Resolve Symbol, Method Contains (java.lang.String), Location: class java.lang.String, while (inputValues != null && !inputValues.contains("."), arrow indicating at the .contains(".") area.
Oh, yes, there is missing closing bracket ) there. Copy it again and now it should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.