Java help, trying to make an array work?

Java help, trying to make an array work?
I’ll post my code, I’d like a little help solving why I”m getting errors. once I make the code work, I can add the math I need to do later. Right now, it’s down to one error, and thats LabOne.java:8: ‘;’ expected
int[] arrayOfNumbers == 2; //The array that will actually hold the numbers //; expected

Here’s my code

import java.util.*; //allows scanner etc.

public class LabOne
{
public static void main(String[] args)
{
int sizeOfArray; //the size of the array. Index will count to this number
int[] arrayOfNumbers == 2; //The array that will actually hold the numbers //; expected

do
{
System.out.println(“Please input an even number less than 10. This is the amount of numbers in the array. “);

Scanner keyboard = new Scanner(System.in);

sizeOfArray = keyboard.nextInt();
}
while (sizeOfArray <= 10 && sizeOfArray >= 2 && sizeOfArray % 2 = 0); //Bitches about boolean and && cannot be applied to boolean. Goes away when Only ONE exists.

System.out.println(“Please input number: “);

for (int index = 0; index < sizeOfArray; index++)
{
System.out.println( "Number :" + (index + 1));
arrayOfNumbers[sizeOfArray] = keyboard.nextInt(); //cannot find symbol method nextInt
}

//i = 1, j = 10. i++ j--
System.out.println("Here are the sums to your array: ");
for (int index = 0; index < sizeOfArray; index++)
{
//do your math here. It should loop.
//this part is for testing
System.out.println( index );
}
System.exit(0);
}
}
The comments are VERY helpful. The only thing I think i need to fix now is a logic problem. How would I make it so it adds sums from the different slots of the array. Like slot 1 and the size of the array. 1 and 10 for example. then 2 and 9, 3 and 8, and so on.

Best answer:

try:
int[] arrayOfNumbers = new int[2];

Tags: , , , ,

Under Forum

1 Comment for Java help, trying to make an array work?

  • 1. Saj  |  August 15th, 2007 at 2:15 pm

    /*
    I edited your code to work. It will let you enter an even number that is in the range of 2 to 10. And that will be the size of your array. Then it will let you enter numbers n times where n is the even number you enetered. Finally, it will sum up the numbers you have enetered and display.

    Hope this helps. Happy Coding!
    */

    import java.util.*;

    public class LabOne
    {
    public static void main(String[] args)
    {
    int sizeOfArray;
    int[] arrayOfNumbers;
    Scanner keyboard = null;

    do
    {
    System.out.println(“Please input an even number less than 10. This is the amount of numbers in the array. “);

    keyboard = new Scanner(System.in);

    sizeOfArray = keyboard.nextInt();
    }
    while ((sizeOfArray > 10 || sizeOfArray < 2) || sizeOfArray % 2 != 0);

    arrayOfNumbers = new int[sizeOfArray];

    System.out.println(“Please input number: “);

    for (int index = 0; index < sizeOfArray; index++)
    {
    System.out.println( “Number :” + (index + 1));
    arrayOfNumbers[index] = keyboard.nextInt(); //cannot find symbol method nextInt
    }

    int sum = 0;
    for (int index = 0; index < sizeOfArray; index++)
    {
    sum = sum + arrayOfNumbers[index];
    }

    System.out.println(“Here is the sum of your array index values: ” + sum);
    System.exit(0);
    }
    }

Leave a Comment for Java help, trying to make an array work?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories