import java.util.Scanner;
public class Separate {
public static void main(String[] args) {
Scanner user_input = new Scanner( System.in );
String variable;
System.out.print("Enter Variable:");
variable = user_input.next();
Separate(variable);
}
public static void Separate(String str) {
String number = "";
String letter = "";
String symbol = "";
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (Character.isDigit(a)) {
number = number + a;
} else {
letter = letter + a;
}
}
System.out.println("Alphabets in string:"+letter);
System.out.println("Numbers in String:"+number);
}
}
Okay, i already have this code which separate the Numbers and Letters that i Input. The problem is, when ever i input Symbols for example (^,+,-,%,*) it also states as a Letter.
What i want to do is to separate the symbol from letters just like what i did on Numbers and Letters and make another output for it.
You are testing if the character isDigit
, else
treat it as a letter.
Well, if it is not a digit, all other cases go to else
, in your code. Create an else
case for those symbols also.