0

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(); } } } 
3
  • 4
    input.charAt(i) == '0' && input.charAt(i) == '1' cannot possibly be true. Hint: AND != OR Commented May 27, 2013 at 17:17
  • 1
    Be smarter than your teacher: use parboiled and its calculator example; understand the code; explain it to your teacher; job done. I still cannot understand why such stupid exercises are asked for in 2013. Commented May 27, 2013 at 17:19
  • Thanks, I changed the && to ||. Commented May 27, 2013 at 17:21

5 Answers 5

2

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

Comments

1

You can split a string as follows,

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

Comments

1

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

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

Comments

0

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 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.