简体   繁体   中英

Inserting values into two array using scanner class in java

import java.util.Scanner;
public class Demo3 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter how many friends: ");
        int numOfFriends = Integer.parseInt(scan.nextLine());
        String arrayOfNames[] = new String[numOfFriends];
        long income[] = new long[numOfFriends];
        for (int i = 0; i < arrayOfNames.length; i++) 
        {
            System.out.print("\nEnter the name of friend " + (i+1) + " : ");
            arrayOfNames[i] = scan.nextLine();
            for(int j = 0; j<arrayOfNames.length;j++)
            {
                System.out.print("\nEnter the income of friend " + (j+1) + " : ");
                income[j] = scan.nextLong();
            }
        }
    }
}

This is my code, I want to take input name from user then the income of that person then again the name of another person The above code is not arranged properly, I think there's problem in the for loop the sample output should be like:

Enter how many friends: 2
Enter name of friend 1 : #############
Enter income of friend 1 : ############## 
Enter name of friend 2 : #############
Enter income of friend 2 : ##############

You should take inner for loop out.

import java.util.Scanner;
public class Demo3
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter how many friends: ");
        int numOfFriends = Integer.parseInt(scan.nextLine());
        String arrayOfNames[] = new String[numOfFriends];
        long income[] = new long[numOfFriends];
        for (int i = 0; i < arrayOfNames.length; i++)
        {
            System.out.print("\nEnter the name of friend " + (i+1) + " : ");
            arrayOfNames[i] = scan.nextLine();
        }
        for(int j = 0; j<arrayOfNames.length;j++)
        {
            System.out.print("\nEnter the income of friend " + (j+1) + " : ");
            income[j] = scan.nextLong();
        }
        scan.close();
    }
}

Besides, if you want to enter them in order, you should just use one for loop

import java.util.Scanner;
public class Demo3
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter how many friends: ");
        int numOfFriends = Integer.parseInt(scan.nextLine());
        String arrayOfNames[] = new String[numOfFriends];
        long income[] = new long[numOfFriends];
        for (int i = 0; i < arrayOfNames.length; i++)
        {
            System.out.print("\nEnter the name of friend " + (i+1) + " : ");
            arrayOfNames[i] = scan.nextLine();
            System.out.print("\nEnter the income of friend " + (i+1) + " : ");
            income[i] = scan.nextLong();
            scan.nextLine();
        }
        scan.close();
    }
}

As a side note, don't forget to close() scanner after its task is completed.

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