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.










share|improve this question























  • for your information, java.lang is imported by default, you do not need to explicitly import it yourself
    – 4dc0
    Nov 10 at 3:53














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.










share|improve this question























  • for your information, java.lang is imported by default, you do not need to explicitly import it yourself
    – 4dc0
    Nov 10 at 3:53












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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();






share|improve this answer






















  • 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










  • 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










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',
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
);



);













draft saved

draft discarded


















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

























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();






share|improve this answer






















  • 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










  • 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














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();






share|improve this answer






















  • 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










  • 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












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();






share|improve this answer














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();







share|improve this answer














share|improve this answer



share|improve this answer








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 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
















  • 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










  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20