Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:45:53+00:00 2026-05-14T05:45:53+00:00

I’m trying to read in a multi line string then split it then print

  • 0

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;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:45:54+00:00Added an answer on May 14, 2026 at 5:45 am

    I wrote already an answer, that was a solution. But you are asking what you are doing wrong.

    1. You are using Eclipse. This is not bad, but if I try to do this in NetBeans the code works a litle bit else because of you are reading and writing in the same loop. So the when I do copy-paste the input in eclipse, it works as you want. But if I copy-paste line by line, it becomes one big mess. So you have to split the read and the write process.
    2. What you are doing wrong is that you first println the result and then (Why? I don’t know. I think you also don’t know…) do a print of each char in the result. So the result is that you have only one good line and the others are started by the previous line. So:

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

      You see? So begin with deleting your inner loop that prints each character.
      When you did this: your output looks like this:

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

      So this is the reason why you thought the foreach loop was the problem. If you look at the end of each outputline (your wrong output: posted in the question), you can see, it was correct.

    3. Then you want in the output a blank line if there was one in the input. But like I wrote in my other answer:

      I hate Scanner

      Scanner does not really read the next line, but the next word. So you have to change it to stdin.nextLine(); Now it works!


    Finally, this is the code you need:

    Scanner stdin = new Scanner(new BufferedInputStream(System.in));
    while (stdin.hasNext()) {
        String line = stdin.nextLine();
        for (String part : line.split("!")) {
            System.out.println(part);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.