0

I wrote a program in BlueJ that accepts a multi-line String and its job is to print that String as a single-line continuous String.

Here's the code I wrote -

    import java.util.Scanner;
    class test
    {
        public static void main() 
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter String: ");
            String s = sc.nextLine();
            while(sc.hasNextLine())
            {
                s+=sc.nextLine();
            }
            System.out.println(s);
        }
    }

When I run this program and give the following input by copy-pasting it into the BlueJ terminal -

10100101111011111111
00000000000000000000
01011101110110101111

the program runs infinitely and gives no output.

To check where the code was going wrong I changed it to -

    import java.util.Scanner;
    class test
    {
        public static void main() 
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter String: ");
            String s = sc.nextLine(), a = s;
            while(sc.hasNextLine())
            {
                System.out.println(s);
                s+=sc.nextLine();            
            }
            System.out.println(s);
        }
    }

However, the output I'm getting is -

10100101111011111111
1010010111101111111100000000000000000000

and after that the program runs infinitely without any output.

Can anyone fix my code so that it gives the proper output -

101001011110111111110000000000000000000001011101110110101111
Lampoon
  • 13
  • 1
  • 4

0 Answers0