简体   繁体   中英

get input from user and store it in String Array in Java

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 在此处输入图片说明

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);
    }
}

}

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);
    }
 }
}

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.

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));
    }

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

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM