-1

So I have two classes:

class ConcatTesting{
    public static void main(String args[]) throws java.io.IOException{
        char inLetter;
        String input="";

        //This loops takes line of cmd and makes the input variable into that string
        for(;;){
            inLetter=(char) System.in.read(); //get next char

            //if the line hasn't ended then add that char to input
            if(inLetter!='\n'){
                input+=String.valueOf(inLetter);
            }else{
                //other wise line has ended so input is finished
                break;
            }
        }

        //removes extra white-spaces
        input.trim();

        //test what input is to make sure it is working correctly
        System.out.println(input);

        //test concat function
        UseConcat.ask(input);

        UseConcat.ask("pie");
    }
}

class UseConcat{
    public static void ask(String str){
        System.out.println("What does " + str +" mean?");
    }
}

In the program I call the static method UseConcat.ask(String str) twice.

When the argument in the UseConcat.ask(String str) is the input variable, the concatenation seems to fail. However, when I call UseConcat.ask(String str) with the argument being a random string, the concatenation works.

The input variable is the first written line of the cmd converted to a string.

Here is an example image.

Example Image

As shown in the image, the input variable is set to WOA.

However UseConcat.ask(input); prints out mean?oes WOA intsead of What does WOA mean?

When input is printed: System.out.println(input); it prints WOA as normal.

On the other hand when I call UseConcat.ask("pie"); It works and prints: What does pie mean?

Zachooz
  • 535
  • 1
  • 12
  • 25
  • Why not use a `Scanner` to capture user input? – Ryan J Dec 19 '14 at 00:06
  • @RyanJ Seems to work with scanner but why is it failing with how I currently do it? – Zachooz Dec 19 '14 at 00:13
  • 1
    I can tell you it has to do with the behavior of the terminal, and not the specific code. It works using an IDE with terminal emulation. I suspect you're being bit by something like what [this post](http://stackoverflow.com/questions/4007534/why-cant-we-read-one-character-at-a-time-from-system-in) describes. I'd suggest switching to `Scanner` though, since it's a little more robust and more in-line with current practice. – Ryan J Dec 19 '14 at 00:24

1 Answers1

0

Text lines on Windows (usually for files but always for console window) end with two characters CR (Carriage Return) and LF (Linefeed), which are (most easily) written \r and \n in Java. You remove only the LF and then output "What does WOA{CR} mean?" and the {CR} character moves the cursor to the left margin and causes the " mean?" part to overwrite the "What d" part. Your input.trim() would have fixed this if you hadn't discarded the result. Other platforms are different; Unix uses only LF, and AIUI MacOSX uses only CR.

The Java methods designed to deal with text like Scanner or more basically BufferedReader.readLine() handle any combination of CR and LF for you, with much less code in your application, and more efficiently; repeatedly doing String += letter is horrendously inefficient for long input -- although if you run this program only in a console window that will limit the input line to some not absurdly huge amount.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70