Java Ascii值

Java Ascii值,java,Java,我正在尝试打印字符串的ascii值,打印后面的下一个字符,同时打印前面的字符。因此,如果用户输入bcd我希望我的程序打印出9899100,还有cde,最后是abc。现在是凌晨3:30,我的大脑因为试图让这些东西工作而有点发狂。以下是我所拥有的: BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); String inputValues3; // User enters 3 character

我正在尝试打印字符串的ascii值,打印后面的下一个字符,同时打印前面的字符。因此,如果用户输入
bcd
我希望我的程序打印出
9899100
,还有
cde
,最后是
abc
。现在是凌晨3:30,我的大脑因为试图让这些东西工作而有点发狂。以下是我所拥有的:

BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
String inputValues3;

// User enters 3 character values here
System.out.println("Enter 3 character values");
inputValues3= input.readLine();
String[] charValues = inputValues3.split("\\s+");

System.out.println(inputValues3);    // prints out user character values
System.out.println();                // needs to print out ascii values
System.out.println();                // needs to print out following value
System.out.println();                // needs to print out preceding value 
我尝试了几种不同的方法,试图将字符设置为整数值,但根据我如何使其工作,出现了几种不同的错误

试试以下方法:

inputValues3= input.readLine();
for (char ch : inputValues3.toCharArray()) { 
    System.out.println((int) ch);
}
您需要这样做:

for (int i = 0, n = inputValues3.length(); i < n; ++i) {
  char c = inputValues3.charAt(i);
  if (!Character.isSpaceChar(c)) {
    System.out.print((int)c);
  }
}
System.out.println();
for(int i=0,n=inputValues3.length();i

例如:迭代字符串中包含的字符,然后跳过空格(使用
character.isSpaceChar
)。否则需要强制转换
System.out.print
将打印字符,而不是其值。

要打印Ascii值,只需将
char
转换为
int
并打印它,然后将字符串转换为字符,然后再将字符转换为int?“现在是凌晨3:30,我的大脑因试图让这些工作而有点兴奋。”-我们去睡觉吧。你不能像凌晨3:30那样学习编程。@Marcus Burkhart:Iterate String获取字符,打印每个字符,将其转换为int并打印给定的ascii码。这是一个家庭作业问题,对吗?