ejadwe23
ejadwe23

Reputation: 11

How to put input into 2 different arrays

I am quite new to Java and am trying to figure out how I would be able to get each alternate input to be stored into different arrays. Within my code, I have an infinite loop which gets the program to keep on asking for an input. I want the inputs to be stored into 2 different arrays. For example, if I was to input 1, 7, 45, 23, 36 I would want 1, 45, and 36 to be stored within the 'valueOne' array and 7 and 23 to be stored within the 'valueTwo' array. Thanks in advance

import java.util.Scanner;

public class arraysTest {
    public static void main(String[] args) {
        double[] valueOne = new double[50];
        double[] valueTwo = new double[50];

        Scanner s = new Scanner(System.in);

        while(true) {
        System.out.println("Input a value");
        int userInput = s.nextInt();
        }
    }
}

Upvotes: 1

Views: 255

Answers (3)

hayrettinm
hayrettinm

Reputation: 72

Basically you can have a flag like isEven etc. Then you can check if the input value is in the odd/even.

And for the Scanner, you have to use the same types with your arrays. Other than you can get type errors

boolean isEven = false;
int oneIterator=0;        //iterate over the arrays.
int twoIterator=0;

while(true) {
   System.out.println("Input a value");
   double userInput = s.nextDouble();
   if (isEven){
      valueOne[oneIterator]=userInput;
      oneIterator++;
   }
   else {
      valueTwo[twoIterator]=userInput;
      twoIterator++;
   }
   isEven = !isEven;    // for your case no matter the value of isEven is, 
                        // just turn opposite 

}

Upvotes: 0

You can try this:

  • I'm not checking array limits

    public static void main(String[] args) {
      double[] valueOne = new double[50];
      double[] valueTwo = new double[50];
    
      Scanner s = new Scanner(System.in);
    
      int currentArray = 1;
      int currentIndexArrOne = 0;
      int currentIndexArrTwo = 0;
    
      while (true) {
         System.out.println("Input a value");
         int userInput = s.nextInt();
    
         if (currentArray == 1) {
              valueOne[currentIndexArrOne] = userInput;
              currentIndexArrOne++;
              currentArray = 2;
          } else {
              valueTwo[currentIndexArrTwo] = userInput;
              currentIndexArrTwo++;
             currentArray = 1;
          }
      }
    }
    

Upvotes: 0

Ankit Verghese
Ankit Verghese

Reputation: 155

This should work

import java.util.Scanner;
import java.util.ArrayList;

public class arraysTest {
    public static void main(String[] args) {
        ArrayList<double> v1 = new ArrayList<double>();
        ArrayList<double> v2 = new ArrayList<double>();

        Scanner s = new Scanner(System.in);
        int i=0;
        while(true) {
            i++
            System.out.println("Input a value");
            int userInput = s.nextInt();
            if(i%2==1) v1.add(userInput);
            else v2.add(userInput);
        }
    }
}

Upvotes: 1

Related Questions