Home > Back-end >  How can we put the numbers we receive from the user into an array at the same time and run them in a
How can we put the numbers we receive from the user into an array at the same time and run them in a

Time:06-08

I want to throw 10 numbers I got from the user into the array at the same time and the sum of the first two elements of these numbers is equal to the third. I want to write a Java program that finds and outputs triplet arrays. For example, when the user enters the numbers '22 14 50 62 36 40 58 71 90', by throwing all these numbers into the array at the same time, the output is; --22 14 36-- 22 40 62-- 22 36 58-- 14 36 50-- 50 40 90-- I want them to give their numbers. The sum of the first two elements of the numbers is equal to the third The method I wrote to find the triple sequences;

 private static void getArrayTriplets(int[] inputArray)
{
    System.out.println("Input Array : " Arrays.toString(inputArray));
     
    System.out.println("Array triplets with sum of first two elements equals third :");
     
    for (int i = 0; i < inputArray.length-2; i  ) 
    {
        for (int j = i 1; j < inputArray.length-1; j  ) 
        {
            for (int k = j 1; k < inputArray.length; k  )
            {
                 
                if (inputArray[i]   inputArray[j] == inputArray[k]) 
                {   
                    System.out.println("[" inputArray[i] ", " inputArray[j] ", " inputArray[k] "]");
                }
                else if (inputArray[i]   inputArray[k] == inputArray[j]) 
                {
                    System.out.println("[" inputArray[i] ", " inputArray[k] ", " inputArray[j] "]");
                }
                else if (inputArray[j]   inputArray[k] == inputArray[i])
                {
                    System.out.println("[" inputArray[j] ", " inputArray[k] ", " inputArray[i] "]");
                }
            }
        }
    }
}

However, I could not manage to run this method by having the user enter those numbers at the same time and throwing them all into an array at the same time. How can I do that?

CodePudding user response:

You have to use scanner class to take input from user.

public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int[] inputArray = new int[10];

        // getting input from user
        for (int i = 0; i < 10; i  )
        {
            System.out.println("enter the value of index "   i);
            inputArray[i] = sc.nextInt();
        }

        // printing the array
        for (int i = 0; i < 10; i  )
        {
            System.out.println("value of index "   i);
            System.out.println(inputArray[i]);
        }
    }

CodePudding user response:

Basically the same answer asl @Fakhar ;-)

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter integers separatd by spaces:");
    String input = scanner.next();
    
    String[] stringArray = input.split(" ");
    int[] inputArray = new int[args.length];
    
    for(int i = 0; i < args.length; i  ) {
      inputArray[i] = Integer.parseInt(args[i]);
    }

    getArrayTriplets(inputArray);
  }

Just that it takes any Array length separated by spaces.

Do these answers solve your problem? If not, you need to give us additional informations...

  • Related