1

i write one program that get input from user as "Enter number of students:" then add the student names into it and print it in console. I write one code that run fine but problem is the loop is already ramble one time the code is not properly working i also want to know that how to get inputs using command line argument without Scanner and store it in String Array

Current Output is like that enter image description here

Here is my code please help and i am in learning phrase of Java

import java.util.Scanner;

public class StringScanner
{
  public static void main(String args[])
  {
    Scanner in = new Scanner(System.in);

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();

    //store into String array

    String studentname[] = new String[totalstudents];

    for(int i = 0; i < studentname.length;i++)
    {
        System.out.println(i);
        System.out.println("Enter Student Names: ");
        studentname[i] = in.nextLine();
    }
    for(String names:studentname)
    {
        System.out.println(names);
    }
}

}

3

5 Answers 5

2

next(): Finds and returns the next complete token from this scanner.

nextLine(): Advances this scanner past the current line and returns the input that was skipped.

Try placing a scanner.nextLine(); after each nextInt() if you intend to ignore the rest of the line.

public class StringScanner
{
  public static void main(String args[])
  {
    Scanner in = new Scanner(System.in);

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();
     in.nextLine();// just to ignore the line
    //store into String array

    String studentname[] = new String[totalstudents];

    for(int i = 0; i < studentname.length;i++)
    {

        System.out.println("Enter Student Names: "+i);
        studentname[i] = in.nextLine();
    }
    for(String names:studentname)
    {
        System.out.println(names);
    }
 }
}
0

You can use array args[] Need not pass number of students there.

So what ever name you pass on command prompt after java <className> shall be stored in this array and you can iterate over it.

0

Add in.nextLine(); after you assign this int totalstudents = in.nextInt();

0

use ArrayList instead of String Array

declare header file

      import java.util.ArrayList;

change your code

      Scanner in = new Scanner(System.in);

    //get the input for number of students:
    System.out.println("Enter The number of students:");
    int totalstudents = in.nextInt();

    //store into arraylist
    ArrayList<String> al = new ArrayList<String>();

    for(int i = 0; i < totalstudents;i++)
    {
        System.out.println(i);
        System.out.println("Enter Student Names: ");
        al.add(in.next());
    }
    for(int i=0; i< al.size(); i++)
    {
        System.out.println(al.get(i));
    }
2
  • it's not working but the ArrayList is good suggestion just change it to ArrayList <String> = new ArrayList<String> and (al.add(in.next())
    – user4162593
    Sep 21, 2015 at 6:05
  • Remove Integer type from ArrayList
    – balaraman
    Sep 21, 2015 at 6:10
0

Try this code:

Scanner in = new Scanner(System.in);

//get the input for number of students:
System.out.print("Enter The number of students:");
int totalstudents = in.nextInt();

//store into String array

String studentname[] = new String[totalstudents];

for(int i = 0; i < studentname.length;i++)
{
    System.out.print("Enter Student " + i + " Name:");
    studentname[i] = in.nextLine();
}
for(int i = 0; i < studentname.length;i++)
{
    System.out.println(studentname[i]);
}
3
  • 1
    Can you provide some information about how your answer works? A little exposition could help people learn rather than just copy and paste. Sep 9, 2019 at 17:56
  • Too add to what @bob0the0mighty had commented Please read this article to get a better understanding of what an answer is. Also, please consider also go through the site tour Sep 9, 2019 at 18:02
  • It gets the number of students from the user and stores it in (totalstudents) then creating the array of (studentname) and the length of the array is the number of students entered by the user. In the first loop, the user enter the name of students and stores them in the array of (studentname). In the second loop, the user displaying the names of the students.
    – Yahya
    Sep 10, 2019 at 5:11

Your Answer

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