0
public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);
    int i;
    int n;
    String a;     
    System.out.println("Enter the Class:");
    a = user_input.next();
    System.out.println("Enter the number of Students:");
    n = user_input.nextInt();
    for (i= 1; i <= n; i++) {
        String g = a + i;
        System.out.println(g);
    }
}

This is my program. It gets user input for the Class and prints the Roll Number for the students.

For example: If the class is 10A and the number of students is 10, it prints a series like 10A1 , 10A2, 10A3 ... 10A10

How do I get the program to store these as elements in an array?

For example:

array[0] = 10A1;
array[1] = 10A2;
array[2] = 10A3; 

etc.

4 Answers 4

1

Your code should look like this:

public static void main (String args[])
{
    Scanner user_input = new Scanner(System.in);
    int i;
    int n;
    String a;
    System.out.println("Enter the Class:");
    a = user_input.next();
    System.out.println("Enter the number of Students:");
    n = user_input.nextInt();
    String []strings = new String[n]; // Creating an are of string with the given number
    for(i= 0; i < n ;){
        strings[i] = a + ++i; // Storing strings on to the array !
        System.out.println(strings[i-1]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can just edit each index in your current for loop:

String[] arr;
for(i=0; i < n ; i++){
  int j = i+1;

  String g = a + j;
  System.out.println(g);

  arr[i] = g;
}

So all your printed g's will be part of the array arr.

1 Comment

Please execute your code ! it will give weird output ! because you are incrementing i twice !
0

First, declare a String array of the appropriate size.

Second, in your for loop, assign the strings you are currently printing, to positions in the array.

String[] things = new String[n];
for (i=1; i <= n; i++) {
    String g = a + i;
    System.out.println(g);
    things[i-1] = g;
}

The strings are now in an array.

Comments

0

Following code is modified, for storing values in array.

public static void main(String[] args) {
            // TODO code application logic here
         Scanner user_input = new Scanner(System.in);
         int i;
         int n;
         String a;     

         System.out.println("Enter the Class:");
         a = user_input.next();
         System.out.println("Enter the number of Students:");
         n = user_input.nextInt();

         String[] arr = new String[n];  // create string array of size n.

         for(i= 1; i <= n ; i++){
            String g = a + i;
            System.out.println(g);
            arr[i-1]=g;  // assign your g veriable vale to array index
        }

         for(String s : arr){
             System.out.println(s);   // print your array
         }

    }

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.