alexthefourth
alexthefourth

Reputation: 137

Java get int from string input

Im writing a binary search tree. The user will use the program as follows:

Which tree would you like to test (BST, ST, RBT)?

BST How many items would you like to insert? 10000 Pattern (random or sorted)? sorted Next command (insert X, delete X, find X, height, quit)? find 111111 Item not present.

For the first three choices i figure i can just use strings to choose between BST, ST and RBT as wel as to choose between random or sorted, somthing like

String choice
if( choice == "random")
  insert random numbers

what im having trouble with is the 4th choice. if the user enters insert 100 as a string, for example, would i just have to take the 100 off and make it an int. and if so, how would i go about doing that?

Upvotes: 0

Views: 5885

Answers (3)

Dafrostie
Dafrostie

Reputation: 21

First thing I want to note is that a String should not be compared with ==, rather you should use string.equals(comparedString); You can also use the following code to parse through all the inputs that a person enters, and then use both the string entered and the string value entered and it would not be dependent on the start of the string. This would satisfy the options for them all; insert, delete, ect.

String userInput;//received by system in
String choice;
int choiceInt;
for (int i = 0; i < userInput.length(); i++) 
{
    Character character = userInput.charAt(i);
    if (!Character.isDigit(character)) 
    {
        choice += character;
    }
    else if (Character.isDigit(character))
    {
        choiceInt += character;
    }
    else
    {
        System.out.println("Unable to determine character.");
    }

    /* Code for the comparison of String based option */
    //Example
    if (choice.equalsIgnoreCase("insert")//NOTE: the ignore case, 
                                         // you want users to be                        
                                         // able to enter in strings 
                                         // and not have a case sensitivity.
    {
        /* do the code you planned on doing here */
    }
}

You could also assign integer values for each String possibility that you are willing to accept as valid options. This would increase the coding but also would also for a switch case statement. (which is still interpreted as if, else if, else statements) I think at that point it would be more up to the developers intentions and design preferences. Please correct me if I am wrong.

You can also replace the final else statement by either using a try and catch block.

Upvotes: 1

Bohemian
Bohemian

Reputation: 424993

Try this:

if (input.startsWith("insert")) {
    int num = Integer.parseInt(input.replaceAll("\\D", ""));

}

Upvotes: 0

jsshah
jsshah

Reputation: 1741

You can use the combination of the functions to determine whether a string is an int or not

public boolean isInteger(String str) {
  try {
    Integer.parseInt(str);
    return true;
  } catch(NumberFormatException e) {
    return false;
  }
}

If this function returns true ... string is an integer ... now get the integer value using

Integer.parseInt(str);

Upvotes: 4

Related Questions