简体   繁体   中英

Looping CharAt to convert a String to ASCII

Hi guys i made this

import java.util.Scanner;
//Creates a class

public class codeString {

    public static void main(String[] arg) { //creates scanne/giving name
        Scanner ImBack = new Scanner(System.in);

        //print out "enter any String" and asks to put in data
        System.out.print("Enter any String :");
        String Word = ImBack.nextLine();

        int ascii = (int) Word.charAt(0);
        System.out.println(ascii);
        System.out.println((char) Word.charAt(0));
    }
}

But when i run it it converts only 1 letter, I know that i have to make a loop.. so then i went on google and made this

for (Word.charAt(0); Word = int; Word = Word) {
    System.out.println("" + Word);
}

printing lots of errors, one of them was asking for toString, but it worked with out the toString for the one letter, so i know i did loop wrong 100%, could anyone help? and will i need a

    length 

in there?

You need something like this :

for (int i = 0; i < Word.length(); i++) {
    System.out.println(Word.charAt(i));
}
  1. Word.length() return to you the length of your word or text
  2. Word.charAt(i) to get character by character

You can learn also the Oracle tutorials about Arrays and do...while Loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM