Output issue Java
Ok i have finally worked out this problem but i have one issue remaining, when i am entering the string in upon being asked, IF i put any spaces in the number i enter when the program runs it turns those spaces into the number 9 in my output, if i do not use spaces everything runs fine,any ideas as to how i can stop the 9 being added into my spaces? Thanks in advance.
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){
if (uppercaseLetter == 'A' || uppercaseLetter == 'B' || uppercaseLetter == 'C')
return 2;
else if (uppercaseLetter == 'D' || uppercaseLetter == 'E' || uppercaseLetter == 'F')
return 3;
else if (uppercaseLetter == 'G' || uppercaseLetter == 'H' || uppercaseLetter == 'I')
return 4;
else if (uppercaseLetter == 'J' || uppercaseLetter == 'K' || uppercaseLetter == 'L')
return 5;
else if (uppercaseLetter == 'M' || uppercaseLetter == 'N' || uppercaseLetter == 'O')
return 6;
else if (uppercaseLetter == 'P' || uppercaseLetter == 'Q' || uppercaseLetter == 'R' || uppercaseLetter == 'S')
return 7;
else if (uppercaseLetter == 'T' || uppercaseLetter == 'U' || uppercaseLetter == 'V')
return 8;
开发者_如何学Python else
return 9;
}
}
Output example: Enter a string: 597 6630
597*9*6630
Well look at what getNumber()
does - if it doesn't match any of the cases you've specified, it returns 9.
Options:
- Change it to return a different number explicitly for space
- Change the calling code to not call
getNumber()
if the value is ' '.
I would also recommend refactoring the calling code to only call charAt(i)
once, just for the sake of tidiness. For example, you might want:
for (int i = 0; i < string.length(); i++){
char c = string.charAt(i);
if (Character.isDigit(c) || c == ' ') {
System.out.print(c);
} else {
System.out.print(getNumber(c));
}
}
As noted in comments, getNumber()
can also be written with a simple switch/case:
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;
default:
return 9;
}
}
(Obviously you could stack the cases vertically should you want to, as well...)
Add an if statement between else
and return 9;
that only outputs 9 for W,X,Y,Z and append else return "";
Slightly off-topic but still important: big switch statements like this are frequently a suggestion that you are missing some kind of object abstraction. I love enums for situations like this, so I would encapsulate this logic in an enum, perhaps like this:
public enum PhoneDigit {
Zero(""), One(""), Two("ABC"), Three("DEF"), Four("GHI"),
Five("JKL"), Six("MNO"), Seven("PQRS"), Eight("TUV"), Nine("");
private final String letters;
private PhoneDigit(String letters) {
this.letters = letters;
}
public boolean hasLetter(char character) {
return letters.contains(String.valueOf(character));
}
public PhoneDigit valueOf(char character) {
for (PhoneDigit digit : values()) {
if (digit.hasLetter(character)) {
return digit;
}
}
return null;
}
}
This code is very easy to test, it's reusable in other parts of your system, etc. Although my "valueOf" method is not as efficient as a switch statement, you can easily improve this later should it turn out to matter, which is highly unlikely.
Now you can add/test other methods like getPhoneNumberFromString(String)
, boolean isPhoneDigit(char)
etc to this object, and not only are the methods very convenient to test, but it makes logical sense to store all of these methods in this location. Your code will be easier to read, the sun will shine brighter, it will be the best thing you've ever done short of buying a SlapChop.
Finally, notice that you pick up useful methods like values()
, ordinal()
etc for free.
Good luck!
精彩评论