using scanner and delimeter
I need to scan a series of lines from a .txt file that looks like this picture
to make an output like the following: "this output
Here is my code:
Scanner scan = new Scanner(new File("C:\a1.txt"));
String wordbuffer="";
while(scan.hasNext()) {
scan.useDelimiter("\Z");
wordbuffer+=scan.next();
System.out.println(wordbuffer);
if(scan.next==" "){
wordbuffer="";
}
}
scan.close();
java java.util.scanner
add a comment |
I need to scan a series of lines from a .txt file that looks like this picture
to make an output like the following: "this output
Here is my code:
Scanner scan = new Scanner(new File("C:\a1.txt"));
String wordbuffer="";
while(scan.hasNext()) {
scan.useDelimiter("\Z");
wordbuffer+=scan.next();
System.out.println(wordbuffer);
if(scan.next==" "){
wordbuffer="";
}
}
scan.close();
java java.util.scanner
1) Don't change delimiter betweenhasNext()
andnext()
. --- 2) Since input is one character per line, including a space on a blank line, usenextLine()
, notnext()
, to read lines.
– Andreas
Nov 20 '18 at 19:38
1
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
Why did you believeZ
(The end of the input but for the final terminator, if any) would be a good delimiter here?
– Andreas
Nov 20 '18 at 19:41
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36
add a comment |
I need to scan a series of lines from a .txt file that looks like this picture
to make an output like the following: "this output
Here is my code:
Scanner scan = new Scanner(new File("C:\a1.txt"));
String wordbuffer="";
while(scan.hasNext()) {
scan.useDelimiter("\Z");
wordbuffer+=scan.next();
System.out.println(wordbuffer);
if(scan.next==" "){
wordbuffer="";
}
}
scan.close();
java java.util.scanner
I need to scan a series of lines from a .txt file that looks like this picture
to make an output like the following: "this output
Here is my code:
Scanner scan = new Scanner(new File("C:\a1.txt"));
String wordbuffer="";
while(scan.hasNext()) {
scan.useDelimiter("\Z");
wordbuffer+=scan.next();
System.out.println(wordbuffer);
if(scan.next==" "){
wordbuffer="";
}
}
scan.close();
java java.util.scanner
java java.util.scanner
edited Nov 20 '18 at 20:10
Djordje Milošević
asked Nov 20 '18 at 19:36
Djordje MiloševićDjordje Milošević
154
154
1) Don't change delimiter betweenhasNext()
andnext()
. --- 2) Since input is one character per line, including a space on a blank line, usenextLine()
, notnext()
, to read lines.
– Andreas
Nov 20 '18 at 19:38
1
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
Why did you believeZ
(The end of the input but for the final terminator, if any) would be a good delimiter here?
– Andreas
Nov 20 '18 at 19:41
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36
add a comment |
1) Don't change delimiter betweenhasNext()
andnext()
. --- 2) Since input is one character per line, including a space on a blank line, usenextLine()
, notnext()
, to read lines.
– Andreas
Nov 20 '18 at 19:38
1
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
Why did you believeZ
(The end of the input but for the final terminator, if any) would be a good delimiter here?
– Andreas
Nov 20 '18 at 19:41
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36
1) Don't change delimiter between
hasNext()
and next()
. --- 2) Since input is one character per line, including a space on a blank line, use nextLine()
, not next()
, to read lines.– Andreas
Nov 20 '18 at 19:38
1) Don't change delimiter between
hasNext()
and next()
. --- 2) Since input is one character per line, including a space on a blank line, use nextLine()
, not next()
, to read lines.– Andreas
Nov 20 '18 at 19:38
1
1
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
Why did you believe
Z
(The end of the input but for the final terminator, if any) would be a good delimiter here?– Andreas
Nov 20 '18 at 19:41
Why did you believe
Z
(The end of the input but for the final terminator, if any) would be a good delimiter here?– Andreas
Nov 20 '18 at 19:41
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36
add a comment |
1 Answer
1
active
oldest
votes
You can try the following:
Add:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
Code:
public static void main(String args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400327%2fusing-scanner-and-delimeter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try the following:
Add:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
Code:
public static void main(String args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
add a comment |
You can try the following:
Add:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
Code:
public static void main(String args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
add a comment |
You can try the following:
Add:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
Code:
public static void main(String args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
You can try the following:
Add:
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
Code:
public static void main(String args) throws IOException {
Scanner in = new Scanner(new FileReader("C:\a.txt"));
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
String s=in.nextLine();
if (s.equals(""))
sb.append(System.lineSeparator());
else
sb.append(s);
}
in.close();
System.out.println( sb.toString());
}
Output:
stack
o2
flow
edited Nov 20 '18 at 20:38
answered Nov 20 '18 at 19:51


NehoraiNehorai
1,014314
1,014314
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
add a comment |
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
sorry dude, it was my mistake, SO format my string, i edited now
– Djordje Milošević
Nov 20 '18 at 20:13
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
@DjordjeMilošević See update
– Nehorai
Nov 20 '18 at 20:38
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
Yeahh it works, thanks a lot dude :)
– Djordje Milošević
Nov 20 '18 at 20:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400327%2fusing-scanner-and-delimeter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1) Don't change delimiter between
hasNext()
andnext()
. --- 2) Since input is one character per line, including a space on a blank line, usenextLine()
, notnext()
, to read lines.– Andreas
Nov 20 '18 at 19:38
1
You forgot to ask a question.
– achAmháin
Nov 20 '18 at 19:38
Why did you believe
Z
(The end of the input but for the final terminator, if any) would be a good delimiter here?– Andreas
Nov 20 '18 at 19:41
please do not post a screenshot of text - hard to read and even harder to copy... (and blocked in some environments)
– Carlos Heuberger
Nov 20 '18 at 20:42
@CarlosHeuberger okay,thanks on advice :)
– Djordje Milošević
Nov 21 '18 at 17:36