Java text to ASCII converter with loop

I am trying to transfer my first program text to an ASCII converter, but I have some problems that are explained inside the code:

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(); } } 
+1
source share
6 answers
 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
source

(Perhaps use Scanner.nextLine() .)

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

This divides all accented characters, such as ĉ , into c and zero length ^ . And then all non-ascii (capital P = non-P) are deleted.

+2
source

Try the following:

  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(); } 

This converts the string to a char array, and then outputs a string representation of each character.

0
source

Replace this line

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

int ascii = (int) convert;

That should work.

0
source

This code will work

 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(); } } 
0
source

Did you miss the type by hovering the character into an integer? try the following:

 int ascii = (int) convert; 
-1
source

Source: https://habr.com/ru/post/1232031/


All Articles