Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loop not reading in the last item

Tags:

java

c

I'm trying to read in a multi line string then split it then print it .. here is the string :

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

11X21b1X
4X1b1X

When I split the string with ! I get this without the last line string :

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 the mistake, why is not reading in the last line? After I read in all lines properly I should go trough each line if I encounter number I should print the next char the n times the number I just read, but that is long way ahead first I need help with this. Thank you

UPDATE :

Here is how the output should look like :

1T1b5T
1T2b1T1b2T
1T1b1T2b2T
1T3b1T1b1T
3T3b1T
1T3b1T1b1T
5T1*1T

11X21b1X
4X1b1X

Here is a solution in C(my friend solved it not me), but I'd stil wanted 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;
} 
like image 312
Gandalf StormCrow Avatar asked Apr 06 '10 12:04

Gandalf StormCrow


People also ask

Why is my while loop not ending?

The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop.

Why my do while loop is not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

Does continue works in while loop?

Description. In contrast to the break statement, continue does not terminate the execution of the loop entirely, but instead: In a while loop, it jumps back to the condition.

Why is my while loop infinite?

Basically, the infinite loop happens when the condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't updated correctly, or aren't updated at all.


2 Answers

You seem to be doing more work than necessary. The Scanner can use a custom delimiter; in your case, you want it to break up the input on either a newline or a bang, so:

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

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

You're doing something wonky with your print statements -- the code above assumes that all you want to do is print out each line. You seem to be trying to do it twice, in your code, so if I've misinterpreted your intent, modify appropriately.

Edit per your UPDATE: Okay, so you want to allow empty tokens and pass them through. That's easy: just modify the delimiter so that it only matches one character, like so:

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

With no asterisk, we'll match only one thing at a time: a bang, or a newline (of any of the three flavors found on different operating systems).

like image 91
Etaoin Avatar answered Sep 22 '22 15:09

Etaoin


Maybe because you didn't hit enter after the last input line?

Update: tested it here and confirmed it. You need to hit enter after the last input line. Only this way the while (stdin.hasNext()) will return true and proceed with the line.

like image 31
BalusC Avatar answered Sep 21 '22 15:09

BalusC