RvDeAsis
RvDeAsis

Reputation: 13

array display method difficulty

newbie here. So i'm testing out arrays, it's the next topic in the tutorial that i have been reading. I made a simple program to specify how many numbers to get or to input. The problem is on the getArray() method, i can't seem to display it. It's giving me an exception in thread error. Any help and advice is much appreciated. :) Sorry for the newbie question. :)) So here's the code.

import java.util.Scanner;

public class ArrayNumbers {

Scanner input = new Scanner(System.in);
int totalNum, counter, count = 0, display;
double arrayNum[];

public void setArrayNum() {
    System.out.print("How many numbers?: ");
    totalNum = input.nextInt();
    double arrayNum[] = new double[totalNum];

    for (counter = 0 ; counter < arrayNum.length ; counter++) {
        System.out.print("Num "+(count = counter + 1)+": ");
        arrayNum[counter] = input.nextInt(); 
    }
}

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();

    System.out.print(arrayNum[display]);
}
}

import java.util.Scanner;

public class MainProgram {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Numbers NumbersObject = new Numbers();
    ArrayNumbers ArrayNumbersObject = new ArrayNumbers();
    String name;
    int choice;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.println("Menu");
    System.out.println("1. Math Operations");
    System.out.println("2. Grade Computation");
    System.out.println("3. Counting Numbers");
    System.out.println("4. Array Numbers");
    System.out.print("Enter choice: ");
    choice = input.nextInt();

    switch(choice) {

    case 1:
        System.out.println("Choose Operation");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Enter choice: ");
        choice  = input.nextInt();

        NumbersObject.setNum();

        if (choice == 1)
            System.out.println("The answers is "+(NumbersObject.getNum1() + NumbersObject.getNum2()));
        else if (choice == 2)
            System.out.println("The answers is "+(NumbersObject.getNum1() - NumbersObject.getNum2()));
        else if (choice == 3)
            System.out.println("The answers is "+(NumbersObject.getNum1() * NumbersObject.getNum2()));
        else if (choice == 4)
            System.out.println("The answers is "+(NumbersObject.getNum1() / NumbersObject.getNum2()));
        else
            System.out.print("Invalid Input!");
        break;

    case 2:
        NumbersObject.setNum();
        System.out.println("Your average grade is "+((NumbersObject.getNum2() + NumbersObject.getNum2()) / 2));
        break;

    case 3:
        System.out.println("Welcome to Counting Numbers!");
        System.out.println("Enter 2 numbers to start and end!");
        NumbersObject.setNum();

        for (int counter = (int) NumbersObject.getNum1() ; counter <= NumbersObject.getNum2() ; counter++) {
            System.out.println(counter);
        }

        System.out.println("End!");
        break;

    case 4:
        ArrayNumbersObject.setArrayNum();
        ArrayNumbersObject.getArray();
        break;

    default:
        System.out.println("Mr/Ms "+name+" you entered an Invalid Choice!");
        break;
    }//end of switch    
}// end of main
}// end of class

Sorry about that, for not including the main.

And here's the error:

Exception in thread "main" java.lang.NullPointerException
at ArrayNumbers.getArray(ArrayNumbers.java:23)
at MainProgram.main(MainProgram.java:67)

Guys, the only i problem i have is in the getArray() It asks for what number in the array i want to see but when i input it, it gives an exception in the thread error. The only problem is how am i going to display the array[number i specified]? Sorry for the newbie questions.

Upvotes: 1

Views: 140

Answers (3)

Alexis C.
Alexis C.

Reputation: 93892

It throws an exception because you create an array of size display and you try to print the element at the display position but arrays are 0 base indexed in Java.

This figure should be useful :

enter image description here

So imagine that display = 10 :

double arrayNum[] = new double[display]; //create an array that can holds 10 elements
System.out.print(arrayNum[display]); //try to access at the 10th element of the array but it doesn't exists !

You don't have to create a new array because you want to display the number in the arrayNum you created in your class.

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    System.out.print(arrayNum[display]);
}

Also you need to check if display is in the correct bounds range [0,...,displayNum.length-1] before trying to access its elements.

Edit :

In setArrayNum() replace double arrayNum[] = new double[totalNum]; by arrayNum = new double[totalNum];

Upvotes: 2

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

You are creating local array double arrayNum[] = new double[display]; within getArray() method.

You need to remove double arrayNum[] = new double[display]; from getArray() method.

After that your method like below.

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    if(display >0 && display <=arrayNum.length){
            System.out.print(arrayNum[display-1]);
    }else{
         System.out.print(display + " is out of range");
    }
}

Upvotes: 0

Jan Zyka
Jan Zyka

Reputation: 17898

Arrays are 0-based. So if you create an array of size display you can access indexes 0..display-1

Upvotes: 0

Related Questions