I am trying to read in a multiline line, then split it and then print. Here is the line:
1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T
11X21b1X
4X1b1X
When I split the line with !, I get this without the last line of the line:
1T1b5T
1T1b5T1T2b1T1b2T
1T2b1T1b2T1T1b1T2b2T
1T1b1T2b2T1T3b1T1b1T
1T3b1T1b1T3T3b1T
3T3b1T1T3b1T1b1T
1T3b1T1b1T5T1*1T
5T1*1T11X21b1X
11X21b1X
Here is my code:
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner stdin = new Scanner(new BufferedInputStream(System.in));
while (stdin.hasNext()) {
for (String line : stdin.next().split("!")) {
System.out.println(line);
for (int i = 0; i < line.length(); i++) {
System.out.print(line.charAt(i));
}
}
}
}
}
Where did I make a mistake, why not read in the last line? After I read all the lines correctly, I have to go through each line, if I run into a number, I should print the next char n times the number I just read, but this is far ahead, first of all I need help in this one. thank you
UPDATE:
Here's what the output should look like:
1T1b5T
1T2b1T1b2T
1T1b1T2b2T
1T3b1T1b1T
3T3b1T
1T3b1T1b1T
5T1*1T
11X21b1X
4X1b1X
Here is the solution in C (my friend decided it wasn’t me), but I would like to do it in JAVA:
#include <stdio.h>
int main (void)
{
char row[134];
for (;fgets (row,134,stdin)!=NULL;)
{
int i,j=0;
for (i=0;row[i]!='\0';i++)
{
if (row[i]<='9'&&row[i]>='1')
j+=(row[i]-'0');
else if ((row[i]<='Z'&&row[i]>='A')||row[i]=='*')
for (;j;j--)
printf ("%c",row[i]);
else if (row[i]=='b')
for (;j;j--)
printf (" ");
else if (row[i]=='!'||row[i]=='\n')
printf ("\n");
}
}
return 0;
}
source
share