Reputation: 11
In my assignment i am supposed to create a program that uses a switch case to access methods from a different class to do something. The first case takes in user input, and that input is used in the constructor of the other class. however if i initialize the class object in the first case, i get errors from the other cases because the object may not have been initialized yet. how do i create that object for the class that has my methods, and still get the user input that is prompted in the switch case into the constructor?
public class NumberList{
int length; int offset;
public NumberList(int length, int offset){ //constructor
this.length = length; this.offset = offset;
}
public void shift(int a){ //shift method
temp = numbers[0];
for (int i = 0; i < length-1; i++) {
numbers[i] = numbers[i+1];
}
numbers[length - 1] = temp;
}
say that is my class with a method to shift array elements. my main method would be
public class assignment7{
public static void main(String[] args){
int choice;
do{
System.out.println("input choice");
choice = scan.nextInt();
switch(choice){
case '1':
System.out.println("input the array size.");
size = scan.nextInt();
System.out.println("input the array offset.");
offset = scan.nextInt();
NumberList numbasbrah = new NumberList(size, offset);
numbasbrah.printInfo();
break;
case '2':
numbasbrah.shift();
numbasbrah.printInfo();
break;
case '3': //quit
break;
}while(choice!=3);
}} //end main method
so if i create the NumberList object in the switch case i get the error "variable may not have been initialized" but it needs to be there so i can add the user input for the constructor. how do i initialize the object while still being able to add the information for the constructor in the switch case?
Upvotes: 0
Views: 107
Reputation: 13910
The issue is you are instantiating a class inside the case 1 scope, nothing outside of that scope will have access to that object. More information on scopes here.
But for your immediate problem if you want the object created in case 1 to be accessible in case 2 just create outside the case but inside the switch (create with default values):
switch(choice){
NumberList numbasbrah =new NumberList(defaultValue, defaultVale);
case '1':
System.out.println("input the array size.");
size = scan.nextInt();
System.out.println("input the array offset.");
offset = scan.nextInt();
numbasbrah = new NumberList(size, offset);
numbasbrah.printInfo();
break;
case '2':
numbasbrah.shift();
numbasbrah.printInfo();
break;
case '3': //quit
break;
}
NOTE
The logic here seems faulty. What if user selects case 2, is there going to be a default numbasbrah?
Upvotes: 1
Reputation: 7919
I think switch case is not required here because case 1
is always required step here .For ex if you define your object as null(as @Eduardo Dennis answer) and then you chosses case 2
it will give you a null pointer and on the other hand if you chosse case 1
you are already initializing object so in both cases you need object initialization the why not remove case 1
and define it outside switch
or the best is to remove switch and use if else
Upvotes: 0
Reputation: 2038
You can't use an object without creating it, that's all you need to know ! So either you declare it in the switch case not in the the first case, and you must be sure that the user will go for the case one before the case two, or you can add a new boolean variable that tells if the object has been created or not and use it in the cases that uses the object to be sure that it has already been created ! That's what I would have done.
Or use if(numbasbrah != null)
as Eduardo says
Upvotes: 0
Reputation: 41208
The problem is that you are using numasbrah in case 2 but creating it in case 1.
You have nothing though that is forcing you to go through case 1 before going into case 2.
If someone selects 2 without ever selecting 1 then the program will fail.
There are any number of ways to fix this - including creating a "default" numasbrah outside the switch statement. Probably the simplest though is just to remove the case 2
completely and run through into it from case 1.
Upvotes: 3