Hi I'm a student who studing this and my teacher ask us to wrote a progame that could get the correct output. The request is the input will always start with an integer k, and after the first input there will be k lines of String followed. that means if the k is 4 and after the input k, there is other 4 line of input and all of these inputs are strings. And the request ask that I need to ignore the first k, only look for the second k and print the k lines input after the second k as the output.
What I had tried is I wrote these code blow
public class CS220_A1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstInt = input.nextInt();
String temp = new String();
for (int i = 0; i < firstInt + 1; i++) {
temp = input.nextLine();
}
int secondInt = input.nextInt();
for(int j = 0; j < secondInt + 1; j++) {
System.out.println(input.nextLine());
}
}
}
The input given as a sample is
4
bGTRYLEsJhStPVDMdmoxZle
ZYEDXeMaGotpjAtpcaJQnHyTDXNnhgYMOpIdMBvn
oTRJptFnaMRZNuapwqWYbXIEOothm
aLKzsZAIuyW
3
oi
oii
oiii
0
and the output should be
oi
oii
oiii
My code works well with this sample, however when I do the test by myself, I found out that if the input didn't end up with a Integer (or maybe a digit) the output will be different. if I let the input as
0
3
asc
azxv
aabbb
The output will only have "asc"and"azxv" comes out, until I press the enter button. But it didnt turns to the right side, after I press the enter button, there will be a empty line shows on the result there which cause my output incorrect.
Not sure if the picture works or not. I decide to put the output down here The incorrect output will shows as below
asc
azxv
and after I press the enter button, it show as
asc
azxv
aabbb
There is a empty line come out there! And I try a lot to fix it but I failed. I want to know how can I fix this problem and I want to know if my idea start with the correct side! Thanks a lot!