about getting user input

+1 Talli . · December 31, 2015
If I have multiple variables and I want to add user input to them without having to user mor the one line, what syntax should I use??
for example, if I want the user to insert a math equation in one line rather than three.
like "2 + 3" 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Nicholas Eason · January 1, 2016
You should take the input as a whole equation, like you wanted, and say:

input = input.trim();


This will remove any spaces from the String. After that:

if(input.indexOf("+") > -1){
int num1 = input.substring(0, input.indexOf("+"));
int num2 = input.substring(input.indexOf("+") + 1, input.length());
int answer = num1 + num2;
System.out.println(answer);
}

You could repeat this whole setup for any operator, just replace the "+" with whatever, i.e for subtraction make them all "-".
+1 Gary Whitney · January 2, 2016
Talli,
Nicholas gave you a very eloquent parsing solution to your specific inquiry.
To learn about the methods he used I suggest reading the source i.e. javadocs string.

P.S.
One advantage to getting the input one variable at a time is you can validate it before going on to the next input.
By validate I mean, identify what the value is before assigning it to a primitive type.
This is easy if you are using Scanner.
For example this method does the validation for you (in. is the Scanner variable):

    public int getAnInteger(){
        while (!in.hasNextInt()){
            in.nextLine();
            System.out.print("That's not an integer. Try again: ");
        }
        return in.nextInt();
    }

You may have to read the javadocs on Scanner to understand this method.
+1 Nicholas Eason · January 3, 2016
Bucky doesn't cover the String methods that I used, as far as I can remember, but like Gary suggested, check out the Javadoc for the String class.

Trim returns a string identical to the one passed to it excluding any whitespace (spaces, tabs, enters)

substring returns a String that is a piece of a String based on the passed parameters, an inclusive index (char is included) and an exclusive index (char is not included), respectively.

indexOf returns the index of the specified character (or String, but I didn't use it that way in the example I provided), useful when you need to find a character at an arbitrary location (such as the + sign for your equation).

Length just returns the length of the String, as in the number of characters (note: not just letters) contained in the String.

Also, side note, just noticed I gave you a semi-incorrect error answer so let me correct it:


if(input.indexOf("+") > -1){
int num1 = Integer.parseInt(input.substring(0, input.indexOf("+")));
int num2 = Integer.parseInt(input.substring(input.indexOf("+") + 1, input.length()));
int answer = num1 + num2;
System.out.println(answer);
}


parseInt will attempt to convert a String to an Integer, however if there is a non-integer value contained in the String then it will throw an exception. Note that you can't just say int = String that was a mistake on my part because I was tired when answering your question.
0 Talli . · January 1, 2016
Thank you but I in in the 16th tutorial so I'm not familiar with almost all of the syntax you typed. Can you tell me the functions of input.trim(), input.substring(), input.indexOf and input.lenght??
0 Talli . · January 4, 2016
Thank you
  • 1

Java / Android Development

107,072 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator