0
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 : ##############
0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I know its a solution but in this I have to insert all the names and then their salaries, but I want to take name of first person then salary of that person, then name of the second person and his salary..... like that.and I don't want to use arraylist..

Your Answer

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