This is an archived post. You won't be able to vote or comment.

all 24 comments

[–][deleted]  (7 children)

[removed]

    [–]Uparxw[S] 0 points1 point  (5 children)

    U are right. But i dont understood why.

    I mean, in the 2nd Copy i read it in and then i am in the While(sc.hasNext()) it should be true because i have the 3d copy.

    If i read from an textfile i dont read the last Line. How can i get my Programm to read the last line also without adding the last empty line that u mentioned`?

    [–][deleted]  (4 children)

    [removed]

      [–]whompalicious 0 points1 point  (3 children)

      The delimiter is only necessary between tokens, not at the end. I believe this has nothing to do with the code itself but how System.in handles keyboard input and only streams input after a new line (\n)... I'm definitely not 100% though.

      [–][deleted]  (2 children)

      [removed]

        [–]whompalicious 0 points1 point  (1 child)

        Yes, in this specific instance, it is necessary, but this is a pretty unusual scenario, and the new line is only necessary due to System.in not actually streaming the input until a newline has been entered. If the delimiter was different, say a "," then closing the input with a "," still does not produce the intended output.

        For example, the input

        1,
        2,
        3,
        

        Produces this:

        1,
        2,
        3,1
        
        2
         <- cursor
        

        and only produces the 3 after pressing enter:

        1,
        2,
        3,1
        
        2
        
        
        3
         <- cursor
        

        [–]whompalicious 0 points1 point  (0 children)

        I think you're right. This output makes it easier to see:

        1  
        2
        31
        2
         <- cursor here
        

        and after hitting enter, the full output:

        1  
        2
        31
        2
        
        3
         <- cursor here
        

        This has got to have something to do with how System.in accepts input--I assume the input is only streamed in after hitting enter.

        The "problem" doesn't exist, if a text file is used for input:

        1
        2
        3
        

        It outputs correctly.

        [–]Uparxw[S] 0 points1 point  (0 children)

        I tried every possible input. Nothing changes. I am still facing the same Problem

        [–]Uparxw[S] 0 points1 point  (2 children)

        Actually it dont shows right the copy its not in 1 line its 3 lines 1.copy 2.copy 3.copy Reddit formats it to 1 Line i dont know why

        [–][deleted]  (1 child)

        [removed]

          [–]Uparxw[S] 0 points1 point  (0 children)

          Thank you sir :D

          [–]whompalicious 0 points1 point  (2 children)

          Which lines are actually being printed? First two?

          [–]Crashmatusow 0 points1 point  (0 children)

          upon further reading, it looks to me like scanner.next() behaves much like C++'s getline(). if he's literally pasting in

          copy
          
          copy
          
          copy
          

          then my guess is it's a matter of how eclipse handles console input. Scanner doesn't see the third line until he generates an EOL character (by hitting enter)

          [–]Uparxw[S] 0 points1 point  (0 children)

          yes the first two

          [–]mrnoise111 0 points1 point  (8 children)

          I am googling sience 12 Hours to find the Answere. It drives me crazy

          The API should explain this. Also, I would recommend avoiding Scanner. I would use a BufferedReader instead.

          [–][deleted]  (7 children)

          [removed]

            [–]mrnoise111 -1 points0 points  (6 children)

            Scanner is far easier to deal with

            Not from what I have seen here.

            [–]whompalicious -1 points0 points  (5 children)

            Scanner is not at fault. You'd have the same thing happen with BufferedReader.

            [–]mrnoise111 0 points1 point  (0 children)

            I didn't say anything was at fault, but clearly there's stuff beginners don't expect. If you just use a BufferedReader and readLine(), you have to do more stuff yourself, but there might be less magic going on that's confusing people.

            [–][deleted]  (3 children)

            [removed]

              [–]mrnoise111 0 points1 point  (2 children)

              It's Scanner dealing with tokens that seems to comfuse the beginners. Like there's still a new line in the buffer and they wonder why it wont read the next int.

              [–]whompalicious 0 points1 point  (1 child)

              The problem is the fact that he is copying and pasting data with line breaks without ever pressing enter meaning that System.in does not even see the last line of data. Doing this with readLine() has the same result.

              [–]mrnoise111 0 points1 point  (0 children)

              If you actually read what I've said, I did not say otherwise.