简体   繁体   English

line = reader.readline()结果与文字“F”的行为不同

[英]line = reader.readline() result does not behave same as a literal “F”

I was trying to develop a command line interface test first using a technique described in Jeff Langr's book Agile Java, but I can not figure out why: 我试图首先使用Jeff Langr的书Agile Java中描述的技术开发命令行界面测试,但我无法弄清楚原因:

line = reader.readline(); line = reader.readline(); result does not behave same as a literal "F" in my code. 结果与我的代码中的文字“F”的行为不同。

This works...rover moves forward to 1: 这有效...流浪者向前移动到1:

private void goForward() {
        rover.move("F");
    }

This does not work...rover remains at 0: 这不起作用...流动站保持在0:

private void goForward() throws IOException {
    StringBuffer input = new StringBuffer();
    input.append(line("F"));
    byte[] buffer = input.toString().getBytes();
    InputStream inputStream = new ByteArrayInputStream(buffer);
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(inputStreamReader);  
    String line = reader.readLine();
    rover.move(line);
}

private String line(String str) {
    return String.format("%s%n", str);
}

I have done everything I can think of: 我做了我能想到的一切:

I added several 我添加了几个

System.out.println(line);

to look at the values for line before and after and everything looks okay. 查看前后行的值,一切看起来都没问题。

But looks can be deceiving so I added an 但看起来可能是欺骗所以我添加了一个

if(line.equals("F"))

thinking this would prove they are actually different but it evaluates to True 认为这会证明它们实际上是不同的,但它的评估结果为True

And spent several hours with my debugger, Javadocs and searching the Internet. 并花了几个小时与我的调试器,Javadocs和搜索互联网。 I have learned a lot along the way but nothing that explains what is happening. 我一路上学到了很多,但没有任何东西可以解释发生了什么。

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 I am sure I am missing some fine nuance that plague novices like myself. 我相信我错过了一些困扰像我这样的新手的细微差别。 Thanks. 谢谢。

Ok, after reading your comment, I have a better answer... (First one was completely wrong, sorry): 好的,看完你的评论后,我有一个更好的答案......(第一个是完全错误的,对不起):

Strings should NEVER be compared via == , because that will only be true if it's the same OBJECT (and not the same STRING). 字符串永远不应该通过==进行比较,因为只有当它是相同的OBJECT(并且不是相同的STRING)时才会出现这种情况。 Use equals() . 使用equals()

It only works with constant "F", because the compiler takes all constant "F"s and uses the same String for them. 它只适用于常量“F”,因为编译器采用所有常量“F”并为它们使用相同的字符串。 But as soon as you have a dynamic String, coming from the "outside" during Runtime, it will be a totally different object, for which == will be FALSE, while equals will be TRUE. 但是只要你有一个动态字符串,在运行时来自“外部”,它将是一个完全不同的对象,其中==将为FALSE,而equals将为TRUE。

String x = "F";
// Might be true, because the compiler will use ONE object 
// for both instances of "F". It's still bad and should not be done.
boolean isF = x == "F";

String y = new Scanner(System.in).nextLine();
// Will NEVER be true, even if you entered "F", because
//  y will always be a NEW String object...
isF = y == "F"; 
// Will be true, if you entered "F"
isF = "F".equals(y);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在reader.readLine()期间检测第一行和最后一行? - How to detect first and last line during reader.readLine()? Java reader.readLine()不会在文件中返回确切的行 - Java reader.readLine() dosn't return the exact line in the file 使用“ while(line = reader.readLine()!= null)”时,类型不匹配 - Type mismatch when using “while(line = reader.readLine() != null)” 使用ProcessBuilder执行Linux命令并从reader.readLine()获取NULL,尽管行数为102 - Using ProcessBuilder to execute Linux Command and getting NULL from reader.readLine() although line count is 102 bufferedReader reader.readline 直接回null - bufferedReader reader.readline directly goes back to null 如何从reader.readLine()返回的String中删除unicode空格 - how to remove unicode spaces from String returned by reader.readLine() 为什么我必须第二次在 while 中写 line = reader.readLine() ? 我以为我已经在上面定义了 - Why do I have to write line = reader.readLine() in while for the second time? I thought I have already defined it above java-服务器未从客户端接收消息(reader.readLine()== null?) - java - server not recieving messages from client (reader.readLine() == null ?) Java 在 reader.readLine() 从服务器读取数据时出现套接字错误 - Java socket error while reading data from server at reader.readLine() 如何在使用reader.readLine()和sc.nextLine()时避免阻塞 - How to avoid blocking while using reader.readLine() and sc.nextLine()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM