0

I have to read strings from the user based on a number n and store n strings in n different variables. I'm stuck with how to put them into different strings. Please help me out.

This is my code:

public static void main(String[] args) {
      int b;
      String s="";

      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();

      for (int i=0; i<=b; i++) {

          System.out.println("Enter a string: ");
          s = in.nextLine();
      }

So if b = 5, i have to input 5 strings from the user and store them in 5 different string variables. I'm able to take it from the user but not able to assign them into different variables. Can u please help me out? Thanks.

9
  • 2
    I would recommend reading about Java data-structures. Maybe you could make use of the ArrayList Commented Jan 10, 2015 at 16:14
  • 2
    Read up on arrays... Commented Jan 10, 2015 at 16:14
  • Use an array or a List<String>. Commented Jan 10, 2015 at 16:14
  • @JoshEngelsma use List<String> and initialize it as ArrayList<String> rather than directly using ArrayList. Commented Jan 10, 2015 at 16:15
  • Are you allowed to use an array, or an ArrayList? Commented Jan 10, 2015 at 16:15

4 Answers 4

6

If you know exactly the number of input then use an Array, if not use a ArrayList

With Arrays

  String []inpupts = new String[b];
  for (int i=0; i< b; i++) {
      System.out.println("Enter a string: ");
      inputs[i] = in.nextLine();
  }

With ArrayList

  List<String> inpupts = new ArrayList<String>();
  for (int i=0; i< b; i++) {
      System.out.println("Enter a string: ");
      inputs.add(in.nextLine());
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I updated your answer a little to correct <= problem (there shouldn't be =). I also renamed b to size which seems more descriptive but that is subjective matter so feel free to edit it back to names used in OP code (OK nvm Luiggi took care of it :D).
Shouldn't it be "in.next()" instead of "in.nextLine();", because nextLine() moves the scanner position to the next line and returns the value as a string, so "Enter a string: " gets printed twice before user can give his input and so only 1 input is taken.
3

From your code (<= b) I am assuming you just started learning Java. Therefore, I edited your solution and am proposing the following, if this is okay?

public static void main(String[] args) {
      int b;


      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();
      //necessary to do due to Enter key pressed by user
      in.nextLine();
      String s[] = new String[b];
      for (int i=0; i<b; i++) {

          System.out.println("Enter a string: ");
          s[i] = in.nextLine();

          // You can check at the same time if this is what you entered
          System.out.println("I have received this sring:  "+s[i]+"\n");
      }

Comments

2

Create an array and store it in an array like below:

String s[] = new String[b];//use b+1 if you need b+1 entries
for (int i=0; i<b; i++) {//use <=b is you need b+1 entries
      System.out.println("Enter a string: ");
      s[i] = in.nextLine();
}

You can then access your values as:

for (int i=0; i<b; i++) //use <=b is you need b+1 entries
      System.out.println("Entered string was : " + s[i]);
}

7 Comments

Why new String[b+1] and not new String[b]?
See OP's for loop, he takes values from user starting at 0 till b. Hence b+1.
That means OP's for loop is wrong due to Scanner.nextInt requiring a call to Scanner#nextLine to consume the Enter pressed by user.
Hmm may be or may be not.. May be OP himself can figure that out which is being not clear here.
Hope @LuiggiMendoza it makes sense now.
|
0

Solution :

You can use something like this.

You can change your code as :

public static void main(String[] args) {
      int b;
      String s="";

      Scanner in = new Scanner(System.in);

      System.out.println("Enter verifying number: ");
      b = in.nextInt();
      in.nextLine(); // To get a new line

      for (int i=0; i<b; i++) {

          System.out.println("Enter a string: ");
          s = in.nextLine();
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.