从输入更改为扫描仪

时间:2013-10-16 22:45:28

标签: java

我正在尝试将其从输入框更改为Scanner类,但我无法这样做。

这是一个程序,它接受单词并将它们变成电话号码就是这样做的代码。任何帮助将不胜感激,如果有什么我可以作为回报,我很乐意这样做。

// declare imports
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
public class Telephone {
    public static void main(String[] args) {
        // ask for the phone number (in letters)
        char letter;
        String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
        String inputString = JOptionPane.showInputDialog(inputMessage);
        String outputString = "";
        String outputMessage = "";
        int digit = 0;
        int x = 0;
        for (int i = 0; i < inputString.length(); i++)
        System.out.print(inputString.charAt(x)); {
            while (inputString.charAt(x) != '#') {
                letter = Character.toUpperCase(inputString.charAt(x));
                x++;
                // make sure its not a number
                if (letter >= 'a' && letter <= 'z') if (letter >= 'A' && letter <= 'Z') {
                    digit++;
                    switch (letter) {
                        case 'A':
                        case 'B':
                        case 'C':
                            outputString += "2";
                            break;
                        case 'D':
                        case 'E':
                        case 'F':
                            outputString += "3";
                            break;
                        case 'G':
                        case 'H':
                        case 'I':
                            outputString += "4";
                            break;
                        case 'J':
                        case 'K':
                        case 'L':
                            outputString += "5";
                            break;
                        case 'M':
                        case 'N':
                        case 'O':
                            outputString += "6";
                            break;
                        case 'P':
                        case 'Q':
                        case 'R':
                        case 'S':
                            outputString += "7";
                            break;
                        case 'T':
                        case 'U':
                        case 'V':
                            outputString += "8";
                            break;
                        case 'W':
                        case 'X':
                        case 'Y':
                        case 'Z':
                            outputString += "9";
                    }
                    if (digit == 7) {
                        break;
                    }
                    if (digit == 3) {
                        outputString += "-";
                    }
                }
                inputMessage = "Enter another set of telephone letters";
            }
            JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);

        }

1 个答案:

答案 0 :(得分:1)

修改显示JOptionPane的行,如下所示,

//String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();

所以你可以做到以下几点,

package test;

import java.util.Scanner;

public class Telephone {

    public static void main(String[] args) {
        // ask for the phone number (in letters)
        char letter;
        String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
//        String inputString = JOptionPane.showInputDialog(inputMessage);

        System.out.println(inputMessage);
        Scanner sc = new Scanner(System.in);
        String inputString = sc.nextLine();

        String outputString = "";
        String outputMessage = "";
        int digit = 0;
        int x = 0;
        for (int i = 0; i < inputString.length(); i++) {
            System.out.print(inputString.charAt(x));
        }

        while (inputString != null && inputString.trim().length() > 0 && inputString.charAt(x) != '#') {
            letter = Character.toUpperCase(inputString.charAt(x));
            x++;
            // make sure its not a number
//                if (letter >= 'a' && letter <= 'z') {
            if (x >= inputString.length()) {
                x = 0;
                System.out.println("\n" + outputString);
//                JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
//                inputString = JOptionPane.showInputDialog(inputMessage);
                System.out.println(inputMessage);
                inputString = sc.nextLine();
            } else if (letter >= 'A' && letter <= 'Z') {
                digit++;
                switch (letter) {
                    case 'A':
                    case 'B':
                    case 'C':
                        outputString += "2";
                        break;
                    case 'D':
                    case 'E':
                    case 'F':
                        outputString += "3";
                        break;
                    case 'G':
                    case 'H':
                    case 'I':
                        outputString += "4";
                        break;
                    case 'J':
                    case 'K':
                    case 'L':
                        outputString += "5";
                        break;
                    case 'M':
                    case 'N':
                    case 'O':
                        outputString += "6";
                        break;
                    case 'P':
                    case 'Q':
                    case 'R':
                    case 'S':
                        outputString += "7";
                        break;
                    case 'T':
                    case 'U':
                    case 'V':
                        outputString += "8";
                        break;
                    case 'W':
                    case 'X':
                    case 'Y':
                    case 'Z':
                        outputString += "9";
                }
                if (digit == 7) {
                    break;
                }
                if (digit == 3) {
                    outputString += "-";
                }
            }
//                }
            inputMessage = "Enter another set of telephone letters";
        }
        System.out.println("\n" + outputString);
//        JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);

    }
}