有关从Scanner读取字符并将其转换为Unicode的错误

时间:2017-11-12 23:52:10

标签: java unicode character variable-assignment symbols

我有一个代码将符号转换为Unicode。我在下面附上了我的代码。

问题是如果我使用没有for循环的代码要求输入几个符号,我输出正确。在要求输入符号后使用for循环时,我收到错误。我试图找到问题,但没有成功。也许很容易找到它,但现在我无法找到它。

任何帮助修复错误都表示赞赏。感谢。

This is my code:

下面,我将代码作为文本,以备你想测试它:

package practicejava;

import java.util.Scanner;

public class UnicodeSymbol 
{
    public static void main(String[] args)
    {
        char symbol;

        Scanner myScanner = new Scanner(System.in);
        // To read a first char or symbol by scanner
        myScanner.useDelimiter("");

        // Ask for the number of input
        System.out.println("How many symbols do you want to try?\t");
        // To validate positive integer input from scanner
        int input;
        do 
        {
            System.out.println("Please enter a positive number!");
            while (!myScanner.hasNextInt()) 
            {
                System.out.println("That's not a positive number!");
                myScanner.next();
            }
            // if the condition is false:
            input = myScanner.nextInt();
        } 
        while (input <= 0);
        System.out.println("Thank you! Your input is: " + input);

        // To convert and print each registered symbol to unicode
        for (int i=0; i <= input; i++)
        {
            System.out.println("Please enter a symbol or character:\t");
            symbol = myScanner.findInLine(".").charAt(0);
            int code = (int) symbol;
            System.out.println("This is the unicode for symbol " + symbol + " :\t" + code);   
        }
        System.exit(0);
    }

}

This is the stack-trace image...

0 个答案:

没有答案