My Question probably does not explain the issue well at all but what I’m experiencing is that I’m able to get the first line of a string for a “box” as shown in the sample here: https://docs.google.com/open?id=0B_ifaCiEZgtcVUx3c2c1VWs2NEE
here’s my main code as it is now:
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 scan = new Scanner(line);
//scan.useRadix(breaker);
//while (scan.hasNext()){
for(int i = 0; i < breaker; i++){
box += scan.next();
}box += "\n";
//}
return box;
}
public String toString()
{
return line + "\n" + getLineBreaker();
}
}
And it’s associated runner class:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;
public class Lab12f
{
public static void main(String args[]) throws IOException
{
LineBreaker test = new LineBreaker("1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9", 4);
out.println(test);
test.setLineBreaker("t h e b i g b a d w o l f h a d b i g e a r s a n d t e e t h", 2);
out.println(test);
test.setLineBreaker("a c o m p u t e r s c i e n c e p r o g r a m", 7);
out.println(test );
test.setLineBreaker("i a m s a m i a m", 2);
out.println(test);
}
}
currently my output is looking like this:
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
1234
t h e b i g b a d w o l f h a d b i g e a r s a n d t e e t h
th
a c o m p u t e r s c i e n c e p r o g r a m
acomput
i a m s a m i a m
ia
Your issue is in this block of code here:
You never tell the program to keep reading from the line after reading to the first break.
EDIT
Ok. Don’t use
scan.useRadix(breaker). That doesn’t apply–useRadix()tells the compiler what base to parse numbers in.You were on the right track with the
whileloop. If you uncomment it, though, it throws an error. This is when the scanner is looking for the next element in the line, but there isn’t one. Check if there’s an element remaining before reading it…And a tip from personal experience: Meeting with your professor when you have a question can make a world of difference… This is a place to ask questions (I’m not saying you are wrong to do so), but I will say that your professor knows you better than I do.