-
Output issue
Hi guys, first off this is homework and i did throw this on another form but am still confused. Pretty much i have to take user input, turn it into uppercase letters that correspond with the phone keypad 2=ABC etc and translate whatever input is letters into those numbers. My program works, problem is when i add a space in my input ex.597 3450, i get back 597*32*3450, when i dont add a space the number comes back as it was put in. I am trying to figure out how to declare "space" but am stumped. Any ideas? Thanks in advance, sorry if i did not explain this too good.
Code:
package chapter_9;
import java.util.Scanner;
public class Nine_Seven {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String string = input.nextLine();
string = string.toUpperCase();
for (int i = 0;i<string.length();i++){
if(Character.isDigit(string.charAt(i))) {
System.out.print(string.charAt(i));
} else
System.out.print(getNumber(string.charAt(i)));}
}
public static int getNumber(char uppercaseLetter){
switch (uppercaseLetter) {
case 'A':
case 'B':
case 'C':
return 2;
case 'D':
case 'E':
case 'F':
return 3;
case 'G':
case 'H':
case 'I':
return 4;
case 'J':
case 'K':
case 'L':
return 5;
case 'M':
case 'N':
case 'O':
return 6;
case 'P':
case 'Q':
case 'R':
case 'S':
return 7;
case 'T':
case 'U':
case 'V':
return 8;
case 'W':
case 'X':
case 'Y':
case 'Z':
return 9;
}
return(uppercaseLetter);
}
}
-
Re: Output issue
What other forum? We'd prefer to see the link so as not to duplicate effort. We are all volunteers doing this on our free time after all.
-
Re: Output issue
-
Re: Output issue
Thank you. Your problem is with Character.isDigit(...). You don't want to exclude just digits, you want to exclude anything that is not ABCDEFGHIJKLMNOPQRSTUVWXYZ. So test for that. Test if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" contains your character (look at the String class methods), and if so, call your method, otherwise display the character unchanged.
-
Re: Output issue
Ok thank you for the help, i have been going round and round with this for what seems like too long, guess it may be the best way to learn though!
-
Re: Output issue
I just wanted to thank you for your help, i finally got it right, i adjusted my if statements and it works great. Thanks again.
Code:
package chapter_9;
import java.util.Scanner;
public class Nine_Seven {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String string = input.nextLine();
string = string.toUpperCase();
for (int i = 0;i<string.length();i++){
if(Character.isAlphabetic(string.charAt(i))) {
System.out.print(getNumber(string.charAt(i)));}
else
System.out.print(string.charAt(i));}
}
public static int getNumber(char uppercaseLetter){
switch (uppercaseLetter) {
case 'A':
case 'B':
case 'C':
return 2;
case 'D':
case 'E':
case 'F':
return 3;
case 'G':
case 'H':
case 'I':
return 4;
case 'J':
case 'K':
case 'L':
return 5;
case 'M':
case 'N':
case 'O':
return 6;
case 'P':
case 'Q':
case 'R':
case 'S':
return 7;
case 'T':
case 'U':
case 'V':
return 8;
case 'W':
case 'X':
case 'Y':
case 'Z':
return 9;
}
return(uppercaseLetter);
}
}