Reputation: 93
I am trying to initialize an array based on user input so it can be of any custom size. By default i have initialized array to 10 with all 0.0 values but when I try call the method that initializes the array nothing happens and the values neither the size or the values in the array change.
Class Numbers:
public class Numbers {
private Float [] numbers;
private int default_size = 10;
public Numbers() {
/// write code here to intialize a "default" array since this is the default constructor
numbers = new Float[default_size];
for(int i = 0; i < default_size; i++)
numbers [i] = (float) 0.0;
}
public Numbers (int size) {
/// write code here to initialize an "initial" array given the parameter size as this is an initial constructor
numbers = new Float[size];
}
public void initValuesInArray() {
/// write code here to intialize the values in the array
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter Value : ");
numbers[i] = scan.nextFloat();
}
}
public String toString() {
// write code to return the values in the array in a list format - one value per line
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);
return " ";
}
public float calcAverage() {
// write code to return the average of the values in the array
double sum = 0.0;
for (int i = 0; i < numbers.length; i++)
sum += numbers[i];
double average = sum / numbers.length;
System.out.println(average);
return (float) average;
}
}
Class Main:
public class Main {
public static void main (String [] args) {
// write the code here to implement the menu as specified in Lab 1
boolean menuLoop = true;
while(true) {
System.out.println("Enter 1 to initialize a default array");
System.out.println("Enter 2 to initialize an array of input size");
System.out.println("Enter 3 fill array with values");
System.out.println("Enter 4 display values in array");
System.out.println("Enter 5 to display average of the values in the array");
System.out.println("6 to quit\n");
Numbers obj = new Numbers();
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
switch (i) {
case 1:
obj = new Numbers ();
break;
case 2:
System.out.println("Enter the new size of array: ");
int x = scan.nextInt();
Numbers [] numbers = new Numbers[x];
break;
case 3:
System.out.println("Enter the float numbers as values in the array: ");
obj.initValuesInArray();
break;
case 4:
obj.toString();
break;
case 5:
obj.calcAverage();
break;
case 6:
System.exit(0);
break;
default :
System.out.println("Invalid Entry...");
break;
}
}
}
}
It needs to be able to change the size of the array based on user input and thereafter the user can enter the values into the array.
EDIT:
I think i have finally figured out the issue here and that is when i use break in the switch statement it initializes the array to default which is 10 with all 0.0 values. Although i am not sure how to make the array hold the input value if the option selected is 2
Upvotes: 2
Views: 699
Reputation: 620
You should not use Array if your amount of elements is not fixed. Instead, use List
or ArrayList
(subclass of List).
You can find all the methods you need at: developer.android.com/reference/java/util/ArrayList
EDIT 24/01/19:
If you need the List
or ArrayList
to be 10 by default just in case the user doesn't input a value, you can create an IF/ELSE
which fills the List automatically or maybe use the SWITCH CASE
you already have
For example:
List<Integer> myList = new ArrayList<Integer>();
float defaultValue = 0.0;
case 1:
for(int i = 0; i < 10; i++) {
myList.add(defaultValue)
}
break;
case 2:
System.out.println("Enter the new size of array: ");
int x = scan.nextInt();
for(int i = 0; i < x; i++) {
myList.add(defaultValue)
}
break;
Upvotes: 1
Reputation: 4209
You're not using the public Numbers (int size)
constructor you've made in the Numbers
class. Also you're creating a new variable called numbers
, rather than assigning the local field obj
. This:
Numbers [] numbers = new Numbers[x];
should be
obj = new Numbers(x);
Upvotes: 1