Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-ASCII转换遇到空格时Printf中断_Java_Printf_Ascii - Fatal编程技术网

Java-ASCII转换遇到空格时Printf中断

Java-ASCII转换遇到空格时Printf中断,java,printf,ascii,Java,Printf,Ascii,我有一个脚本,它应该获取数字和字母的字符串,并将它们分解为相应列下的ASCII/Hex值。它完美地工作,直到我在绳子的任何地方放了一个空格。它会将所有内容正常打印到空白处,然后不会出现错误 Ex. (works): kdillon76 Ex. (does NOT work): kdillon 76 在For循环中,我有一个If语句,说明如果字符是一个数字,那么“dothis”后跟一个Else语句来覆盖任何“Else”。Else语句不应该将空格转换为“32”ASCII码吗 非常感谢您的任何帮助

我有一个脚本,它应该获取数字和字母的字符串,并将它们分解为相应列下的
ASCII/Hex
值。它完美地工作,直到我在绳子的任何地方放了一个空格。它会将所有内容正常打印到空白处,然后不会出现错误

Ex. (works):
kdillon76

Ex. (does NOT work):
kdillon 76

在For循环中,我有一个If语句,说明如果字符是一个数字,那么“dothis”后跟一个Else语句来覆盖任何“Else”。Else语句不应该将空格转换为“32”ASCII码吗

非常感谢您的任何帮助

  import java.util.*; // Load all Utility Classes

public class DKUnit3Ch12 { // Begin Class DKUnit3Ch12

    public static void main(String[] args) { // Begin Main

        Scanner myScan = new Scanner(System.in); // Initialize the Scanner
        String myInput; // Define a new Variable

        System.out.print("Please enter a string of any length: "); //Print the text
        myInput = myScan.next(); // Define a new Variable with the next user input

        System.out.printf("%n%-8s%-16s%-16s%s%n", "Initial", "ASCII(char)", "ASCII(int)", "Hex"); // Print the labels with proper tablature

        for(int x = 0; x < myInput.length(); x++) { // Begin For Loop

            char myChar = myInput.charAt(x); // Define a new Variable based on position in index
            if(Character.isDigit(myChar)) { // Begin If Statement (if the character is a digit)
                System.out.printf("%-24s%-16d%02X%n", myChar, (int)myChar, (int)myChar); // Print the items with proper tablature including capitalized Hex
            } // End If Statement
            else { // Begin Else Statement (if the character is NOT a digit)
                System.out.printf("%-8s%-32d%02X%n", myChar, (int)myChar, (int)myChar);  // Print the items with proper tablature including capitalized Hex
            } // End Else Statement

        } // End For Loop

        System.out.print("\nThank you for playing!"); // Print the text

        myScan.close(); // Close the Scanner

    } // End Main

} // End Class DKUnit3Ch12
import java.util.*;//加载所有实用程序类
公共类DKUnit3Ch12{//开始类DKUnit3Ch12
公共静态void main(字符串[]args){//Begin main
Scanner myScan=new Scanner(System.in);//初始化扫描仪
字符串myInput;//定义一个新变量
System.out.print(“请输入任意长度的字符串:”;//打印文本
myInput=myScan.next();//使用下一个用户输入定义一个新变量
System.out.printf(“%n%-8s%-16s%-16s%s%n”,“首字母”,“ASCII(字符)”,“ASCII(int)”,“十六进制”);//用适当的表格打印标签
for(int x=0;x
来自:


扫描仪使用定界符模式将其输入拆分为标记,默认情况下,定界符模式匹配空白。

myScan.next()
替换为
myScan.nextLine()

锁定它。我是Java新手,似乎有很多像这样的小东西我都错过了。非常感谢你!Else语句不应该将空格转换为“32”ASCII数字吗?:您应该理解Java不使用ASCII。请看。我理解这一点,但当时我认为Else语句会捕获任何非数字的内容并找到其ASCII值。我肯定会保存你提供的链接以备将来参考。谢谢因此,如果我理解我在这里做错了什么:“myScan.next()”将只返回空格之前的内容,而“myScan.nextLine()”在返回当前行后会使扫描仪向下颠簸。我是否也能够通过使用“Character.isWhitespace()”方法找到ASCII值(尽管代码更多)?