Skip to main content

"2345 * 2341"

String s = "2345 * 2341"; 

String1 = 2345in the following way:

String2 = 2341

String String1 = 2345; String String2 = 2341; 
 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();  }  } } 

}

"2345 * 2341"

String1 = 2345

String2 = 2341

 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(); } } 

}

String s = "2345 * 2341"; 

in the following way:

String String1 = 2345; String String2 = 2341; 
 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();  }  } } 
Source Link
CrizR
  • 688
  • 1
  • 6
  • 26

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:

"2345 * 2341"

String1 = 2345

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(); } } 

}