Question

I'm taking an AP java class. The assignment is to have two arrays set to store up to 10,000 integers. That's easy enough. But that's not where I need help. It has the user input what numbers to store in each array, entering one integer at a time. Entering a negative number, such as -4, is what tells the program to stop entering numbers into that array. So if you enter -1 into arrayone, instead of adding it it's going to move onto arraytwo. In arraytwo it'd move on to the next bit. What I'm having trouble with is the output. This is my code:

import java.io.*;
import static java.lang.System.*;

import java.util.Scanner;
import java.lang.Math;

class Main{

     public static void main (String str[]) throws IOException {
       Scanner scan = new Scanner(System.in);

          int arrayone [] = new int[10000];
          int arraytwo [] = new int[10000];
          {
          for (int i = 0; i < arrayone.length; i++)
          {
          System.out.println("Enter integers ");
          while(arrayone[i] >= 0) {
          arrayone[i] = scan.nextInt();
          }
          {
          System.out.println("\nEnter the values for the second array, up to 10000 values, enter a negative number to quit");
          while(arraytwo[i] >= 0) {
          arraytwo[i] = scan.nextInt();
          }
          }
          System.out.println("First Array:\n");
          System.out.print(arrayone[i] + " ");
          break;
          }
          }
     }
}

While not done yet, the code, when run, needs to have an output like this:

First Array:

(any entered integers for arrayone in numerical order)

Second Array:

(any entered integers for arraytwo in numerical order)

Merged Array:

(combined integers from arrayone and arraytwo in numberical order)

My trouble is this: So far I have it set to only output what's in arrayone, except it only outputs one number: the final one you enter....which is a negative number and I don't want stored.

Any guidance I could have would be awesome. Thank you!

No correct solution

OTHER TIPS

You have a lot of curley braces there... I've stripped out the likely harmful braces and given your code proper indentation. Corrected your loops and exit criteria.

You need to work on your formatting and tracking your opening and closing braces, brackets etc. It will make your code easier for others to follow and easier for you to debug. You do not put braces around EVERY section of code you separate. They should only be used in specific places.

class Main
{

    public static void main (String[] args) throws IOException
    {
        Scanner scan = new Scanner(System.in);

        int[] arrayone = new int[10000];
        int[] arraytwo = new int[10000];

        System.out.println("Enter integers ");
        int userNumber;
        for (int i = 0; i < arrayone.length; i++)
        {
            arrayone[i] = scan.nextInt();
            if (arrayone[i] < 0)
            {
              break;
            }
        }

        System.out.println("\nEnter the values for the second array, up to 10000 values, enter a negative number to quit");
        for (int i=0; i<arraytwo.length; i++)
        {
            arraytwo[i] = scan.nextInt();
            if (arraytwo[i] < 0)
            {
                break;
            }
        }


        System.out.println("First Array:\n");
        for (int i=0; i< arrayone.length; i++)
        {
            System.out.print(arrayone[i] + " ");
        }


        System.out.println("Second Array:\n");
        for (int i=0; i< arraytwo.length; i++)
        {
            System.out.print(arraytwo[i] + " ");
        }

    }
}

I didn't do all the work for you but you should now be in a better place to solve the rest yourself.

You need to print inside your loop. The value is only printed once because it is outside the loop and therefore it will only print once.

 while(arraytwo[i] >= 0) {
          arraytwo[i] = scan.nextInt();
System.out.print(arrayone[i] + " ");
          }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top