Adding Strings with Choppers
up vote
1
down vote
favorite
I am trying to get an output that looks like this:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
The first line being a String of numbers (line) and the last line being all of the numbers printed out in increments of 4 (breaker). Here is the code I got so far (I am using two different files or programs or whatever because of my teacher).
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreaker
private String line;
private int breaker;
public LineBreaker()
this("", 0);
public LineBreaker(String s, int b)
line=s;
breaker=b;
public void setLineBreaker(String s, int b)
line=s;
breaker=b;
public String getLine()
return line;
public String getLineBreaker()
String box = "";
Scanner chopper = new Scanner(line);
while (chopper.hasNext())
for (int i=0;i<breaker;i++)
box+=chopper.next();
box=box+"n";
return box;
public String toString()
return getLine()+"n"+getLineBreaker()+"n";
(the next one is in the separate file)
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreakerRunner
public static void main(String args)
LineBreaker test = new LineBreaker("1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9",4);
System.out.println(test);
Here is the error message that I am getting:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at lab12_scanner_chopper.LineBreaker.getLineBreaker(LineBreaker.java:41)
at lab12_scanner_chopper.LineBreaker.toString(LineBreaker.java:49)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at lab12_scanner_chopper.LineBreakerRunner.main(LineBreakerRunner.java:16)
C:UsersHarrisonAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
The error is pointing to the line in LingBreaker
where it says box+=chopper.next();
So I think that the problem is me adding the chopped bit to a string. But I don't really get why that is a problem. I need to use a Scanner/chopper to get credit.
java java.util.scanner
add a comment |
up vote
1
down vote
favorite
I am trying to get an output that looks like this:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
The first line being a String of numbers (line) and the last line being all of the numbers printed out in increments of 4 (breaker). Here is the code I got so far (I am using two different files or programs or whatever because of my teacher).
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreaker
private String line;
private int breaker;
public LineBreaker()
this("", 0);
public LineBreaker(String s, int b)
line=s;
breaker=b;
public void setLineBreaker(String s, int b)
line=s;
breaker=b;
public String getLine()
return line;
public String getLineBreaker()
String box = "";
Scanner chopper = new Scanner(line);
while (chopper.hasNext())
for (int i=0;i<breaker;i++)
box+=chopper.next();
box=box+"n";
return box;
public String toString()
return getLine()+"n"+getLineBreaker()+"n";
(the next one is in the separate file)
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreakerRunner
public static void main(String args)
LineBreaker test = new LineBreaker("1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9",4);
System.out.println(test);
Here is the error message that I am getting:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at lab12_scanner_chopper.LineBreaker.getLineBreaker(LineBreaker.java:41)
at lab12_scanner_chopper.LineBreaker.toString(LineBreaker.java:49)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at lab12_scanner_chopper.LineBreakerRunner.main(LineBreakerRunner.java:16)
C:UsersHarrisonAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
The error is pointing to the line in LingBreaker
where it says box+=chopper.next();
So I think that the problem is me adding the chopped bit to a string. But I don't really get why that is a problem. I need to use a Scanner/chopper to get credit.
java java.util.scanner
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to get an output that looks like this:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
The first line being a String of numbers (line) and the last line being all of the numbers printed out in increments of 4 (breaker). Here is the code I got so far (I am using two different files or programs or whatever because of my teacher).
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreaker
private String line;
private int breaker;
public LineBreaker()
this("", 0);
public LineBreaker(String s, int b)
line=s;
breaker=b;
public void setLineBreaker(String s, int b)
line=s;
breaker=b;
public String getLine()
return line;
public String getLineBreaker()
String box = "";
Scanner chopper = new Scanner(line);
while (chopper.hasNext())
for (int i=0;i<breaker;i++)
box+=chopper.next();
box=box+"n";
return box;
public String toString()
return getLine()+"n"+getLineBreaker()+"n";
(the next one is in the separate file)
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreakerRunner
public static void main(String args)
LineBreaker test = new LineBreaker("1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9",4);
System.out.println(test);
Here is the error message that I am getting:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at lab12_scanner_chopper.LineBreaker.getLineBreaker(LineBreaker.java:41)
at lab12_scanner_chopper.LineBreaker.toString(LineBreaker.java:49)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at lab12_scanner_chopper.LineBreakerRunner.main(LineBreakerRunner.java:16)
C:UsersHarrisonAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
The error is pointing to the line in LingBreaker
where it says box+=chopper.next();
So I think that the problem is me adding the chopped bit to a string. But I don't really get why that is a problem. I need to use a Scanner/chopper to get credit.
java java.util.scanner
I am trying to get an output that looks like this:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
The first line being a String of numbers (line) and the last line being all of the numbers printed out in increments of 4 (breaker). Here is the code I got so far (I am using two different files or programs or whatever because of my teacher).
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreaker
private String line;
private int breaker;
public LineBreaker()
this("", 0);
public LineBreaker(String s, int b)
line=s;
breaker=b;
public void setLineBreaker(String s, int b)
line=s;
breaker=b;
public String getLine()
return line;
public String getLineBreaker()
String box = "";
Scanner chopper = new Scanner(line);
while (chopper.hasNext())
for (int i=0;i<breaker;i++)
box+=chopper.next();
box=box+"n";
return box;
public String toString()
return getLine()+"n"+getLineBreaker()+"n";
(the next one is in the separate file)
import java.util.Scanner;
import static java.lang.System.*;
public class LineBreakerRunner
public static void main(String args)
LineBreaker test = new LineBreaker("1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9",4);
System.out.println(test);
Here is the error message that I am getting:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at lab12_scanner_chopper.LineBreaker.getLineBreaker(LineBreaker.java:41)
at lab12_scanner_chopper.LineBreaker.toString(LineBreaker.java:49)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at lab12_scanner_chopper.LineBreakerRunner.main(LineBreakerRunner.java:16)
C:UsersHarrisonAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
The error is pointing to the line in LingBreaker
where it says box+=chopper.next();
So I think that the problem is me adding the chopped bit to a string. But I don't really get why that is a problem. I need to use a Scanner/chopper to get credit.
java java.util.scanner
java java.util.scanner
edited Nov 10 at 16:12
Mikhail Kholodkov
3,66252343
3,66252343
asked Nov 10 at 3:46
Steven.Cramer
83
83
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53
add a comment |
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
That is a lot of code for what appears to be a simple problem. I would print the String
. I would use a regular expression to remove all of the white space. Then I would iterate the input and increment by 4 characters at a time. Like,
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", s1 = s.replaceAll("\s+", "");
int len = s1.length();
System.out.println(s);
for (int i = 0; i < len; i += 4)
System.out.print(s1.charAt(i));
if (i + 1 < len)
System.out.print(s1.charAt(i + 1));
if (i + 2 < len)
System.out.print(s1.charAt(i + 2));
if (i + 3 < len)
System.out.print(s1.charAt(i + 3));
System.out.println();
Outputs (as requested)
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
And if you need same with a Scanner
then (for the same result), do something like
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9";
System.out.println(s);
Scanner sc = new Scanner(s);
while (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
System.out.println();
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean aScanner
? If so, you should edit your question to include that little detail.
– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
That is a lot of code for what appears to be a simple problem. I would print the String
. I would use a regular expression to remove all of the white space. Then I would iterate the input and increment by 4 characters at a time. Like,
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", s1 = s.replaceAll("\s+", "");
int len = s1.length();
System.out.println(s);
for (int i = 0; i < len; i += 4)
System.out.print(s1.charAt(i));
if (i + 1 < len)
System.out.print(s1.charAt(i + 1));
if (i + 2 < len)
System.out.print(s1.charAt(i + 2));
if (i + 3 < len)
System.out.print(s1.charAt(i + 3));
System.out.println();
Outputs (as requested)
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
And if you need same with a Scanner
then (for the same result), do something like
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9";
System.out.println(s);
Scanner sc = new Scanner(s);
while (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
System.out.println();
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean aScanner
? If so, you should edit your question to include that little detail.
– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
add a comment |
up vote
0
down vote
accepted
That is a lot of code for what appears to be a simple problem. I would print the String
. I would use a regular expression to remove all of the white space. Then I would iterate the input and increment by 4 characters at a time. Like,
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", s1 = s.replaceAll("\s+", "");
int len = s1.length();
System.out.println(s);
for (int i = 0; i < len; i += 4)
System.out.print(s1.charAt(i));
if (i + 1 < len)
System.out.print(s1.charAt(i + 1));
if (i + 2 < len)
System.out.print(s1.charAt(i + 2));
if (i + 3 < len)
System.out.print(s1.charAt(i + 3));
System.out.println();
Outputs (as requested)
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
And if you need same with a Scanner
then (for the same result), do something like
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9";
System.out.println(s);
Scanner sc = new Scanner(s);
while (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
System.out.println();
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean aScanner
? If so, you should edit your question to include that little detail.
– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
That is a lot of code for what appears to be a simple problem. I would print the String
. I would use a regular expression to remove all of the white space. Then I would iterate the input and increment by 4 characters at a time. Like,
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", s1 = s.replaceAll("\s+", "");
int len = s1.length();
System.out.println(s);
for (int i = 0; i < len; i += 4)
System.out.print(s1.charAt(i));
if (i + 1 < len)
System.out.print(s1.charAt(i + 1));
if (i + 2 < len)
System.out.print(s1.charAt(i + 2));
if (i + 3 < len)
System.out.print(s1.charAt(i + 3));
System.out.println();
Outputs (as requested)
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
And if you need same with a Scanner
then (for the same result), do something like
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9";
System.out.println(s);
Scanner sc = new Scanner(s);
while (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
System.out.println();
That is a lot of code for what appears to be a simple problem. I would print the String
. I would use a regular expression to remove all of the white space. Then I would iterate the input and increment by 4 characters at a time. Like,
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", s1 = s.replaceAll("\s+", "");
int len = s1.length();
System.out.println(s);
for (int i = 0; i < len; i += 4)
System.out.print(s1.charAt(i));
if (i + 1 < len)
System.out.print(s1.charAt(i + 1));
if (i + 2 < len)
System.out.print(s1.charAt(i + 2));
if (i + 3 < len)
System.out.print(s1.charAt(i + 3));
System.out.println();
Outputs (as requested)
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
5678
9012
3456
789
And if you need same with a Scanner
then (for the same result), do something like
String s = "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9";
System.out.println(s);
Scanner sc = new Scanner(s);
while (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
if (sc.hasNextInt())
System.out.print(sc.nextInt());
System.out.println();
edited Nov 10 at 4:12
answered Nov 10 at 3:55
Elliott Frisch
150k1388173
150k1388173
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean aScanner
? If so, you should edit your question to include that little detail.
– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
add a comment |
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean aScanner
? If so, you should edit your question to include that little detail.
– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
Thank you but I have to use a chopper to get the output.
– Steven.Cramer
Nov 10 at 3:57
@Steven.Cramer Do you mean a
Scanner
? If so, you should edit your question to include that little detail.– Elliott Frisch
Nov 10 at 4:03
@Steven.Cramer Do you mean a
Scanner
? If so, you should edit your question to include that little detail.– Elliott Frisch
Nov 10 at 4:03
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
I think I mean Scanner, I did edit the question after I realized I forgot to mention it. I'll change it to say Scanner/Chopper. (I don't really know the terminology very well)
– Steven.Cramer
Nov 10 at 4:06
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53235839%2fadding-strings-with-choppers%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
for your information, java.lang is imported by default, you do not need to explicitly import it yourself
– 4dc0
Nov 10 at 3:53