Until the loop is read in the last element

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;
} 
+5
source share
9

, . , .

  • Eclipse. , NetBeans, litle bit else - , . , - eclipse, , . , . .
  • , , println , (? . , ...) print char . , , , - . :

    [line 1]: foo
    [line 2]: foobar
    [line 3]: barbaz
    [line 4]: bazbam
    

    ? , .
    : :

    1T1b5T
    1T2b1T1b2T
    1T1b1T2b2T
    1T3b1T1b1T
    3T3b1T
    1T3b1T1b1T
    5T1*1T
    11X21b1X
    4X1b1X
    

    , , foreach. ( : ), , .

  • , , . , :

    , . stdin.nextLine(); !


, , :

Scanner stdin = new Scanner(new BufferedInputStream(System.in));
while (stdin.hasNext()) {
    String line = stdin.nextLine();
    for (String part : line.split("!")) {
        System.out.println(part);
    }
}
+3

, , , . Scanner ; , , , :

Scanner stdin = new Scanner(System.in);
stdin.useDelimiter("[!\\s]*"); // Break on any combination of whitespace characters and !s

while (stdin.hasNext()) {        // While there a next token,
    String line = stdin.next();  // read it in
    System.out.println(line);    // and print it out.
}

- - , , , . , , , , , .

UPDATE: , . : , :

stdin.useDelimiter("(!|\\r?\\n|\\r)")

: ( , ).

+15

, , enter?

: . enter . while (stdin.hasNext()) true .

+10

Java SE 6 java.util.Scanner.next():

. , . , hasNext() true.

, Java- , .

+2
public static List<String> ppp(final String source) {
    final ArrayList<String> result = new ArrayList<String>();
    final Scanner scan = new Scanner(source);
    scan.useDelimiter("!");
    while (scan.hasNext()) {
    result.add(scan.next());
    }
    return result;
}

private static void printout(final List<String> someArgs) {
    final Iterator<String> i = someArgs.iterator();
    while (i.hasNext()) {
    System.out.println(i.next());
    }
} 
public static void main(final String[] args) throws IOException {
    final String lineSeparator = System.getProperty("line.separator");
    final String test = "1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T"
            + lineSeparator + lineSeparator + "11X21b1X" + lineSeparator + "4X1b1X" + lineSeparator;
    System.out.println("Source: ");
    System.out.println(test);
    System.out.println("Result as List: ");
    System.out.println(ppp(test));
    System.out.println("Result in lines:");
    printout(ppp(test));}

String - LineSeparators. ppp , "!". prinout stdout .

main() :

Source: 
1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T

11X21b1X
4X1b1X

Result as List: 
[1T1b5T, 1T2b1T1b2T, 1T1b1T2b2T, 1T3b1T1b1T, 3T3b1T, 1T3b1T1b1T, 5T1*1T

11X21b1X
4X1b1X
]
Result in lines:
1T1b5T
1T2b1T1b2T
1T1b1T2b2T
1T3b1T1b1T
3T3b1T
1T3b1T1b1T
5T1*1T

11X21b1X
4X1b1X


+1

, , . , "" , , "4X1b1X", . .

C:\>java Main < test.txt
1T1b5T
1T1b5T1T2b1T1b2T
1T2b1T1b2T1T1b1T2b2T
1T1b1T2b2T1T3b1T1b1T
1T3b1T1b1T3T3b1T
3T3b1T1T3b1T1b1T
1T3b1T1b1T5T1*1T
5T1*1T11X21b1X
11X21b1X4X1b1X
4X1b1X
0

, . ... (, )

Personnaly, Scanner. BufferedReader.

, . , agressif. , .

, :

public static void main(String[] args) throws IOException {
    List<String> lines = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("How many lines count the input? ");
    int count = Integer.parseInt(reader.readLine());
    for (int i = 0; i < count; i++)
    {
        lines.add(reader.readLine());
    }
    System.out.println("\nNow comes the output:");
    for (int i = 0; i < count; i++)
    {
        String line = lines.get(i);
        String[] splitted = line.split("!");
        for (String string : splitted) {
            System.out.println(string);
        }
    }
}

:

How many lines count the input? 4
1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T

11X21b1X
4X1b1X


Now comes the output:
1T1b5T
1T2b1T1b2T
1T1b1T2b2T
1T3b1T1b1T
3T3b1T
1T3b1T1b1T
5T1*1T

11X21b1X
4X1b1X
BUILD SUCCESSFUL (total time: 10 seconds)
0

. . java.io.Reader , .

java

0

The problem is with the scanner. It seems that all you want to do is read the lines. For this, you do not need a scanner (since it will not return the last part, which is not limited). Just use the readLine buffer reader:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {

    public static void main(String args[]) {
        BufferedInputStream stdin = new Scanner(new BufferedInputStream(System.in));
        String line;
        while ((line=stdin.nextLine)!=null) {
            for (String token : line.split("!")) {
                System.out.println(token);
            }
        }
    }

}
0
source

All Articles