First, a note: Instead of concatenating String objects you should look into the StringBuilder class.
Up vote 0 down vote favorite share g+ share fb share tw.
I'm writing a java program that will require a long String as input from the user. Most of these Strings are news articles copied and pasted off the internet. The problem I've having is that when the user enters the text, it contains carriage returns that Scanner has trouble processing.
I'm using the nextLine() method from the Scanner class and the problem is that the carriage returns mark the end of lines and thus terminate that read from the Scanner. I've tried the following code, too, but that results in an infinite loop. UserInput2 is a Scanner.
While(userInput2.hasNextLine()) toBeProcessed = toBeProcessed + userInput2.nextLine(); Anyone know how I can skip over the carriage returns? Maybe have the scanner convert them to a '+' or something like that so I don't have this problem. Java string java-util-scanner carriage-return link|improve this question edited Jul 19 '11 at 19:08Binyamin Sharet37.8k52554 asked Jul 19 '11 at 18:59Kurtis1.
– Ziyao Wei Jul 19 '11 at 19:03 Well, not exactly. The scanner doesn't recognize the end of the input text when I use the while loop. – Kurtis Jul 19 '11 at 19:08 You can try input the end of input twice, which worked for me when I encountered the same problem.
– Ziyao Wei Jul 19 '11 at 19:09.
First, a note: Instead of concatenating String objects you should look into the StringBuilder class. Second, you could use useDelimiter to something that will never be in the input string, or a nicer method would be to use the Apache commons IOUtils with the InputStream as in this question rather than using a Scanner, which gives you the benefit of specifying a character encoding. You can strip '\r' characters with String.replace().
So do not use Scanner. Use InputStream directly and convert bytes to strings.
No matter what method you are using, you must have some sign of input termination. In the Scanner case, you could uuseDelimiter to set the delimiter of the Scanner to whatever you want. Also you could take a look here and here.
You know that there was a newline there because Scanner has used it to determine the end of the line it retrieved. All you have to do is to reinsert what you know was there: while(userInput2.hasNextLine()) toBeProcessed = toBeProcessed + userInput2.nextLine() + "\n"; Martin's comment about using StringBuilder is correct. That would change your code to look like: while(userInput2.hasNextLine()) { toBeProcessedSB = toBeProcessedSB.
Append(userInput2.nextLine()); toBeProcessedSB = toBeProcessedSB. Append("\n"); } toBeProcessed = toBeProcessedSB.toString().
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.