[英]Java program not printing all ASCII values
我已經編寫了一個Java程序,該程序打印我給出的輸入語句的相應ASCII值。 問題是,它沒有打印所有值。
public class test {
public static void main(String[] args)
{
System.out.println("Enter the string to be converted");
Scanner input = new Scanner(System.in);
String str =input.next();
char ch[]=str.toCharArray();
int i =0;
for(i =0;i<str.length();i++)
{
int ascii=(int)ch[i];
System.out.println(ascii+"\n");
}
input.close();
}
}
例如:“最古老的古典希臘文和拉丁文文字之間幾乎沒有或沒有空格,可以寫成文字”,它只印有84,104,101。
它應該打印句子中的所有值。
采用
String str =input.nextLine();
獲取ASCII
值的ASCII
一種方法。
String s = "The oldest classical Greek";
byte[] bytes = s.getBytes("US-ASCII");
for(int i=0;i<bytes.length;i++)
System.out.println(bytes[i]);//prints ASCIIs
String str = input.next();
應該:
String str = input.nextLine();
查找並返回此掃描儀的下一個完整令牌。 完整的標記在其前面,然后是與定界符模式匹配的輸入。
使該掃描儀前進到當前行之外,並返回被跳過的輸入。 此方法返回當前行的其余部分,但不包括最后的任何行分隔符。 該位置設置為下一行的開始。
您應該使用scanner.nextLine()
而不是scanner.next()
(從此掃描器中查找並返回下一個完整的令牌)。
更改為
String str = input.nextLine();
代替
String str = input.next();
input.next()
讀取令牌,而不是您期望的行。
您需要使用
String str = input.nextLine();
代替
String str = input.next();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.