2

I am trying to make a simple Java program where you input 15 numbers (INTS, positive and negative) first, let's say these will get loaded into arrayOne. After that all numbers that are below '-5' need to be loaded into a second array (arrayTwo). I want to print all numbers of arrayTwo, while still retaining all arrayOne numbers.

I know my code doesn't make any sense at all, as I am still a beginner (about a month on and off). This is my code so far:

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        int[] arrayOne = new int[15];
        int count = 0;

        System.out.println("Input 15 ints:  ");

        for (int i = 0; i <= arrayOne.length-1; i++){
            arrayOne[i] = scanner.nextInt();
            if (arrayOne[i] < -5){
                count++;
            }
        }

        int[] arrayTwo = new int[count];

        for (int i = 0; i <= arrayOne.length-1; i++){
            if (arrayOne[i] < -5){
                arrayOne[i] = arrayTwo[i];
            }

        }


    }
}

It's so confusing for me. I don't know what to do to be honest. Do I need to use some kind of nested loop?

Thank you so much in advance, any help will be greatly appreciated.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Sam
  • 35
  • 3
  • I suggest you take some time to learn how to debug your own code. This will help you understand what your code is actually doing and see how that differs from what you thought it would do when you wrote it. It will also help you avoid those same mistakes in the future. Check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for some tips to get started debugging your code. – Code-Apprentice Jan 03 '20 at 17:19
  • IDEs such as Eclipse and Netbeans let you step through your code and inspect variables, making it easier to see what is happening. – chippies Jan 03 '20 at 17:36
  • in last for loop you have to change line arrayOne[i] = arrayTwo[i]; to arrayTwo[i] = arrayOne[i]; , because arrayTwo will contain < -5 numbers – Meet Patel Jan 24 '20 at 11:55

4 Answers4

3
int[] arrayTwo = new int[count];
int index = 0;
for (int i = 0; i <= arrayOne.length-1; i++){
    if (arrayOne[i] < -5){
         arrayTwo[index++] = arrayOne[i];
    }
}

index will be used to write into the second array.

index++ is postfix increment operator. You can read about that here - Java: Prefix/postfix of increment/decrement operators?

You were doing the assignment the wrong way. It must be arrayTwo[..] = arrayOne[..] to assign a value from arrayOne into arrayTwo.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

I know my code doesn't make any sense at all, as I am still a beginner

You don't need to apologize for being a beginner. We were all there once. Your code has the general right idea. You are just missing a few key concepts.

Look at this line:

arrayOne[i] = arrayTwo[i];

You are assigning values from arrayTwo into arrayOne. However, from what you say, you want it the other way around:

arrayTwo[i] = arrayOne[i];

But now you are assigning to the same index in arrayTwo as you are reading from in arrayOne. This will leave 0 values where the original values in arrayOne were larger than -5. I doubt this is what you want.

Instead, you should use two separate indexes for each array. Maybe i1 and i2. These will increment independently. That is i1 will always increment in the for loop because you are stepping over the elements of arrayOne. But i2 should only increment when you write a value into arrayTwo.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

In arrayOne you have 15 integers.But in arrayTwo you have integers that are below -5. Then you have to understand that size of the arrayTwo is less than(in many cases) or equal to size of arrayTwo.Reason is that the integers below -5 is a subset of integers. Therefore using same iterator using while loop for two arrays will make an error.

instead using a second for loop , modify the code as below,

int indexArrayOne = 0, 
indexArrayTwo=0;

while(indexArrayOne < arrayOne.length){
    if(arrayOne[indexArrayOne] < -5){
       arrayTwo[indexArrayTwo++] = arrayOne[indexArrayOne];
    }
    indexArrayOne++;
}

You can use a for loop like what you have done for this.The idea is you have to use two iterators for two arrays.

Erandana
  • 46
  • 7
0

Use ArrayList for arrayTwo, so that you can hav all element in arrayTwo in just single for loop.

List<Integer> arrayTwo = new ArrayList<>();

for (int i = 0; i <= arrayOne.length-1; i++){
    if (arrayOne[i] < -5){
        arrayTwo.add(arrayTwo[i]);
    }
 }

you can use streams and lambda in this code.but first you have learn Stream API & lambda

Meet Patel
  • 482
  • 4
  • 12