首页 文章

如何在同一个字符串中为字母和数字创建单独的输出并输出其ASCII值?

提问于
浏览
-2
import java.util.Scanner; 

public class testing
{
     public static void main(String[] args)
     {
        Scanner stdIn = new Scanner(System.in);
        System.out.println("Please enter a string of any length: "); 
        String convertMe = stdIn.next(); 

System.out.printf("%s%15s%15s%10s%n","Initial","ASCII<char>","ASCII<int>",
"Hex"; 

    for(int x=0; x<convertMe.length(); x++)
    {
       int ascii = (int)convertMe.charAt(x); 
       System.out.printf("%c %30d %13x%n", convertMe.charAt(x),ascii,ascii); 
    }
    System.out.printf("%n"); 
    System.out.println("Thank you for playing!"); 

 }
}

现在代码输出一列中的所有ASCII值 . 我试图根据char是字母还是数字将该列拆分为两列 . 我一直在玩isDigit,但还没能让它上班 . 因为它是一个字符串,我需要它在我的for循环中用于修复后增量以检查是否为true然后在char列中输出如果int列中的false输出可能效果最好我只是不明白如何使这个工作仍然很新编码 .

编辑1:我忘了描述程序LOL . 程序获取字符串的输入并将字符串打印为char,ascii值和十六进制 . 如果char是char列下的字母打印和int下的数字 . 空格也应该作为ASCII和HEX读取空间 .

编辑2:正在玩arraylists并让它运作良好,但我不认为这是一个可行的选择,因为它将使字符串字符无序显示它们因为我必须在同一声明中打印这里是我在哪里 .

public class JMUnit3Ch12
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.println("Please enter a string of any length: "); 
String convertMe = stdIn.nextLine(); 
System.out.printf("%s%15s%15s%10s%n","Initial","ASCII<char>", "ASCII<int>", "Hex"); 
ArrayList<Integer>numbers = new ArrayList<>();
ArrayList<Integer>letters = new ArrayList<>();

for(int x=0; x<convertMe.length(); x++)
  {
      int ascii = (int)convertMe.charAt(x);
      if(ascii<=57 && ascii>=48)
      {
          numbers.add(ascii); 
        }
        else
        {
          letters.add(ascii); 
        }

  //System.out.printf("%c %30d %13x%n", convertMe.charAt(x),ascii,ascii); 

  }

  for(int y :numbers)
  {
  System.out.printf("%c %30d %13x %n",y,y,y); 
}
for(int z: letters)
{
  System.out.printf("%c %15d %28x %n",z,z,z); 
}

  //System.out.println(numbers); 
System.out.printf("%n"); 
System.out.println("Thank you for playing!"); 

  }
}

1 回答

  • 2

    显然,在迭代字符串然后利用 printf() 格式字符串来容纳其中一个或多个字符串时,确定用户输入字符串中的哪些字符是字母字符或数字字符这是一件简单的事情 .

    要查看String字符是Alpha字符还是数字字符,您可以使用 String.matches() 方法,该方法使用Regular Expressions (RegEx)进行确定 . 在下面的示例代码中(这是您的代码修改)我们使用 \d 表达式,它基本上查看我们're matching against is numeric. If it is, it produces boolean true and if not it produces boolean false. You will notice in the code below that we'使用 \d 而不是 \d 的字符(字符串),这是因为反斜杠字符(\)是其中的一部分Expression被称为转义字符,在String中使用时也必须进行转义 . 所以基本上,你必须逃脱逃脱角色 . 你最终会发现 String.matches() 方法对于特定场合来说是一个非常方便的方法 .

    如果发现某个字符是Alpha,则使用一种格式,如果它是Numeric,则使用不同的格式 . 将所需的格式放入String变量中,然后在 printf() 方法中使用该变量 .

    此外,我不知道这是否也在您的IDE中,但您错过了控制台输出中的右括号[ ) ]以显示表格 Headers . 这 should 创建了一个编译错误 .

    这个:

    System.out.printf("%s%15s%15s%10s%n","Initial","ASCII<char>","ASCII<int>","Hex";
    

    应该:

    System.out.printf("%s%15s%15s%10s%n","Initial","ASCII<char>","ASCII<int>","Hex");
    

    在任何情况下:

    Scanner stdIn = new Scanner(System.in);
    System.out.println("Please enter a string of any length: "); 
    // Read the input string as a complete line
    String convertMe = stdIn.nextLine(); 
    
    // Establish a Header in console display
    System.out.printf("%s%15s%15s%10s%n", "Initial", "ASCII<char>", "ASCII<int>", "Hex");
    // Create a header underline in console display
    System.out.println(String.join("", Collections.nCopies(29, "\u2500")));
    
    String displayFormat;
    for(int x = 0; x < convertMe.length(); x++) {
        String c = Character.toString(convertMe.charAt(x));
        // Create a display format to accomodate whether
        // or not the character is alpha or numeric.
        // Is the character numeric?
        if (c.matches("\\d")) {
            displayFormat = "%4c %28d %13x %n";
        }
        // Nope..it's a alpha character
        else {
            displayFormat = "%4c %13d %28x %n";
        }
        int ascii = c.charAt(0); 
        System.out.printf(displayFormat, convertMe.charAt(x), ascii, ascii); 
    }
    System.out.printf("%n"); 
    System.out.println("Thank you for playing!");
    

相关问题