0

How to print multiple Strings from Scanner as there is no array to call out and I'm stuck on how to generate the output.

My do-while loop will stop when user enters #. It seems fine until the part to print out the result, if I put the strResult I can only see the last string of user input.

Below is my current code :

import java.util.Scanner;
public class SecretMessage {

    public static void main(String[] args) {
        int count =0;
        int i =0;
        //char ch;<<<<<<<<<< I need this for converting the string to char
        Scanner sc = new Scanner(System.in);
        //String result[] = new String[count];<<<<<<<<purposedly for array 
        String input ="";
        String strResult ="";


        do  {
            System.out.print("Enter your text :");
            input=sc.nextLine();
            count++;
        }

        while (!input.equals("#"));
        {
            System.out.print("Result :\n");

            strResult = "Case #"+(count-1)+" :"+input;
            //result[count] = strResult;<<<<<<to represent all result
        }       

        for (i=0;i<(count-1);i++) {

            System.out.println(" "+strResult);

        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Aliff
  • 3
  • 3
  • I'm not sure what you're trying to do, to print each of the Strings read in order? But using an array? Or by **not** using it? – Frakcool Oct 03 '17 at 22:05
  • "as there is no array" - is there something blocking you from creating one? Btw: as you can not know the number of lines beforehand, an ArrayList may be a better choice. – tevemadar Oct 03 '17 at 22:06
  • To @Frakcool -Yes, I need to print each of the Strings entered in order. I don't know how the array works in this do-while cases but in For loop I know how the array works. – Aliff Oct 03 '17 at 22:23
  • TO @tevemadar - I still didn't understand how ArrayList works. As you can see in my coding, I to do something else with the input Strings before I can print out the results. For know I just need to get the result printed sequentially. – Aliff Oct 03 '17 at 22:28

2 Answers2

1

You can use an ArrayList

    ArrayList list = new ArrayList();


    do  {
       System.out.print("Enter your text :");
       input=sc.nextLine();
       list.add(input);
       count++;
       }

   while (!input.equals("#"));
{
       System.out.print("Result :\n");

       strResult = "Case #"+(count-1)+" :"+input;
       //result[count] = strResult;<<<<<<to represent all result
       }       

   for (i=0;i<list.size()-1;i++) {

       System.out.println("Case #" +  (i+1) + " " + list.get(i));

   }
user2023608
  • 470
  • 5
  • 18
0

Use StringBuilder: In Java, how to append a string more efficiently?

    StringBuilder stringBuilder = new StringBuilder();

    do  {
        System.out.print("Enter your text :");
        input=sc.nextLine();
        stringBuilder.append(input);
        count++;
    }

    while (!input.equals("#"));
    {
        System.out.print("Result :\n");

        strResult = "Case #"+(count-1)+" :"+stringBuilder.toString();
        //result[count] = strResult;<<<<<<to represent all result
    }  
WilliamNHarvey
  • 2,335
  • 2
  • 17
  • 26