【问题标题】:Java text to ASCII converter with a loop带有循环的 Java 文本到 ASCII 转换器
【发布时间】:2014-09-23 07:22:49
【问题描述】:

我正在尝试将我的第一个程序文本转换为 ASCII 转换器,但我遇到了一些问题,代码中对此进行了解释:

import java.util.Scanner;

public class AsciiConverter {

    public static void main(String[] args){
        System.out.println("Write some text here");
        Scanner scanner = new Scanner(System.in).useDelimiter("\\n"); // Scans whole text
        String myChars = scanner.next();
        int lenght = myChars.length(); // Checking length of text to use it as "while" ending value  
        int i = -1;
        int j = 0;
        do{
            String convert = myChars.substring(i++,j++); // taking first char, should be (0,1)...(1,2)... etc 
            int ascii = ('convert'/1); // I'm trying to do this, so it will show ascii code instead of letter, error: invalid character constant
            System.out.print(ascii); // Should convert all text to ascii symbols
        }
        while(j < lenght );
        scanner.close();
    }
}

【问题讨论】:

  • 为什么你在' ' 中使用'convert'?它是变量的名称。写int ascii = (convert/1);
  • int ascii = ('convert'/1);这行搞糊涂了,那该怎么办?
  • 获取 ASCII 码作为结果的技巧 :)
  • charAt 将在字符串中的给定位置返回 char 值。 char 是一个 16 位无符号数字,它是字符的数字代码。因此,System.out.println((int) myString.charAt(index)); 将在index 处打印字符的数值。
  • int ascii = ('convert'/1); 在 java 8 中无法编译

标签: java


【解决方案1】:
String x = "text"; // your scan text
for(int i =0; i< x.getLength(); x++){
    System.out.println((int)x.charAt(i)); // A = 65, B = 66...etc...
}

【讨论】:

    【解决方案2】:

    您是否错过了将字符转换为整数的类型? 试试这个:

    int ascii = (int) convert;
    

    【讨论】:

    • convert 是一个字符串。
    【解决方案3】:

    试试这个:

        public static void main(String[] args){
            System.out.println("Write some text here");
            Scanner scanner = new Scanner(System.in).useDelimiter("\\n"); // Scans whole text
            String myChars = scanner.next();
            char[] charArray = myChars.toCharArray();
            for (char character : charArray) {
                System.out.println((int)character);
            }
            scanner.close();
        }
    

    这会将字符串转换为 char 数组,然后打印出每个字符的字符串表示形式。

    【讨论】:

    • 很高兴为您提供帮助 :) 如果某个答案解决了您的问题,通常您应该单击它旁边的箭头以接受答案 :)
    【解决方案4】:

    替换这一行

    "int ascii = ('convert'/1);"
    

    int ascii= (int)convert;

    这应该有效。

    【讨论】:

    • 如何将 String 转换为 int??
    【解决方案5】:

    (也许使用Scanner.nextLine()。)

    import java.text.Normalizer;
    import java.text.Normalizer.Form;
    String ascii = Normalizer.normalize(myChars, Form.NFKD)
            .replaceAll("\\P{ASCII}", "");
    

    这会将所有重音字符(如ĉ)拆分为c 和零长度^。然后把所有非ascii(大写P = non-P)都去掉。

    【讨论】:

      【解决方案6】:

      此代码将起作用

      import java.util.Scanner;
      
      public class AsciiConverter {
      
      public static void main(String[] args){
          System.out.println("Write some text here");
          Scanner scanner = new Scanner(System.in).useDelimiter("\\n"); // Scans whole text
          String myChars = scanner.next();
          int lenght = myChars.length(); // Checking length of text to use it as "while" ending value  
          int i = -1;
          int j = 0;
          do{
              String convert = myChars.substring(i++,j++); // taking first char, should be (0,1)...(1,2)... etc 
              int ascii= (int)convert; // I'm trying to do this, so it will show ascii code instead of letter, error: invalid character constant
              System.out.print(ascii); // Should convert all text to ascii symbols
          }
          while(j < lenght );
          scanner.close();
      }
      }
      

      【讨论】:

      • 究竟如何将 String 转换为 int??
      猜你喜欢
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2019-11-12
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 2019-01-04
      相关资源
      最近更新 更多