CrizR
CrizR

Reputation: 688

How can I split a string as such...?

In my programming class my teacher asked us to make a calculator that works in a 'number function number' type format. When writing a method to check if the string was valid I realized it stops checking the string after the space. How can I continue to check it? The only Idea I had was to split it and then check it, but I don't know how to do that, so...

How do I split a string like this:

String s = "2345 * 2341";

in the following way:

String String1 = 2345;
String String2 = 2341;

Then how can I check each string to make sure it is valid? So far I have: (Note: I am a beginner at programming)

   public boolean validNumber() throws IOException {
    input = getUserInput();
    boolean valid = false;
    int i = 0;
    for (valid = true; i < input.length();) {
        if (input.charAt(i) == '0' 
                && input.charAt(i) == '1' 
                && input.charAt(i) == '2' 
                && input.charAt(i) == '3' 
                && input.charAt(i) == '4' 
                && input.charAt(i) == '5' 
                && input.charAt(i) == '6' 
                && input.charAt(i) == '7' 
                && input.charAt(i) == '8' 
                && input.charAt(i) == '9') {
            valid = true;

        }else{
            valid = false;
        }
        i++;
    }

    return valid;
}



public void check() throws IOException {
    boolean valid = validNumber();
   int i = 0;
    while (valid = true && i < 1){

    if (input.contains(" + ")) {
        plus();
    } else if (input.contains(" - ")) {
        minus();
    } else if (input.contains(" / ")) {
        divide();
    } else if (input.contains(" * ")) {
        multiply();

    }else {
        System.out.println("Error Incorrect Input");
        System.out.println("Reinput your numbers");

    }
        check();

        }
    }
}

Upvotes: 0

Views: 172

Answers (5)

tik
tik

Reputation: 135

Below is a sample code:

String String1 = null;
String String2 = null;
String strNumber = "1234*4321";
String delimeter = "\\*";
if(strNumber != null)
{
    String[] parts = strNumber.split(delimeter);
    if(parts.length == 2)
    {
    String1 = parts[0];
    String2 = parts[1];
    }
}

Hope it helps.

Upvotes: 0

Jerome
Jerome

Reputation: 1519

If you are guaranteed to always have a well formed string of the form <operand1> <operator> <operand2>, you can seperate the different parts in the following manner:

String expression = "2345 * 2341";
String[] expressionParts = s.split(" ");

System.out.println(expressionParts[0].equals("2345")); //prints True
System.out.println(expressionParts[1].equals("*")); //prints True
System.out.println(expressionParts[2].equals("2341")); //prints True

Upvotes: 0

Rahul Bobhate
Rahul Bobhate

Reputation: 5082

You can use the split method to split the strings. To check whether the splitted strings are valid, use Long.parseLong method. If the string is not a valid number, it will throw a NumberFormatException.

    String s = "2345 * 23s41";
    String[] strings = s.split("\\*");    //Split strings
    string1 = strings[0].trim();
    string2 = strings[1].trim();

You can check the validity by the following method:

public boolean isValid(String s)
{
    try
    {
        Long.parseLong(s);
    }
    catch(NumberFormatException e)
    {
        return false;
    }
    return true;
}

Upvotes: 2

Pacane
Pacane

Reputation: 21471

If you want to remove the spaces you can do this aswell instead of trimming :

String s = "2345 * 2341";
String[] strings = s.split(" \\* ");

Upvotes: 1

Deepu
Deepu

Reputation: 7610

You can split a string as follows,

String string = "1234*4321";
String[] strings = string.split("\\*");
String string1 = strings[0]; 
String string2 = strings[1]; 

Upvotes: 1

Related Questions