如何允许Java从扫描器中接收/n并打印ASCII值



我正在做codeZinger上的java项目,我需要从扫描仪中获取字符值并打印ASCII值。到目前为止,我的代码除了"/n"的性格。在这种情况下,codezinger在线程"main"中返回错误"Exception"。java.lang.NullPointerException scanner" .

我已经附上了我的代码下面,我已经尝试了一切,它不会工作。我是从c++开始学java的。

我甚至尝试使用if语句手动测试/n,但这不起作用

public class Solution {
public static void main(String[] args) {

//creating input stream
Scanner input = new Scanner(System.in);

// allows character input from input stream
char charIn = input.findInLine(".").charAt(0);
if(input.equals("\n"))
{
System.out.print("10");
}
// casts character to type int to get ascii value

int intVal = (int)charIn;  


System.out.print(intVal);
}
}

Java中的input.equals()方法从不接受撇号形式的参数。请再看一遍:https://www.jquery-az.com/learn-java-equals-method-5-examples/

而且/n在java中不存在。如果你必须打印一行(休息一下),那么你必须使用System.out.println(),简单地说,它将在下一行打印。

对于打印ASCII值也要执行此操作https://www.javatpoint.com/how-to-print-ascii-value-in-java

int code;
while ((code = System.in.read()) != -1) {
System.out.format("0x%02X ", code);
}

最新更新