Java 将句子转换为ASCII格式的代码';当有空格字符时不工作
我想把一个句子转换成ASCII,但是当输入是一个带空格的句子时,输出是空的 如果只是一个单词,代码将输出预期的ASCII码 总的来说,编写的代码将接收来自用户的输入,转换字符串输入,将转换后的句子写入文本文件并从中读取Java 将句子转换为ASCII格式的代码';当有空格字符时不工作,java,ascii,Java,Ascii,我想把一个句子转换成ASCII,但是当输入是一个带空格的句子时,输出是空的 如果只是一个单词,代码将输出预期的ASCII码 总的来说,编写的代码将接收来自用户的输入,转换字符串输入,将转换后的句子写入文本文件并从中读取 String sentence; int[] convert; int l; Scanner s = new Scanner(System.in); System.out.print("Enter a sentence: ");
String sentence;
int[] convert;
int l;
Scanner s = new Scanner(System.in);
System.out.print("Enter a sentence: ");
sentence=s.nextLine();
l=sentence.length();
convert = new int[l];
for(int i=0; i<l; i++){
convert[i]=sentence.charAt(i);
}
//Write to text file
try{
PrintWriter os = new PrintWriter(new FileOutputStream("data.txt"));
for(int i=0; i<l; i++){
os.print(convert[i]);
}
os.close();
}catch(IOException e){
System.out.println("Problem with file output");
}
//Read from text file and print
int num;
try{
Scanner is = new Scanner(new FileInputStream("data.txt"));
while(is.hasNextInt()){
num= is.nextInt();
System.out.print(num);
}
System.out.println("");
is.close();
}catch(FileNotFoundException e){
System.out.println("File was not found");
}
字符串语句;
int[]转换;
int l;
扫描仪s=新的扫描仪(System.in);
System.out.print(“输入句子:”);
句子=s.nextLine();
l=句子长度();
转换=新整数[l];
对于(int i=0;i代码中的以下更改将打印整个输出:
while(is.hasNext()){System.out.println(is.next());}@user207421“…当输入是带空格的句子时,输出为空。”这解决了问题。谢谢!