Results 1 to 10 of 10
Thread: Help with switch cases
- 11-06-2010, 12:26 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Help with switch cases
case 1: System.out.print("How large should I make your array? ");
int size = reader.nextInt();
dblArray = getDblArray(size);
break;
case 4: printArray(dblArray);
break;
I'm having trouble with these switch case statements. case 4 is calling a method (printArray) that prints out all of the values in its array parameter, while case 1 is setting the number of values in the array I'm printing (using the method getDblArray to create the double values in the array). How do I get dblArray in case 4 initialized so it recognizes getDblArray(size) as the array? All of this must be in the main method.
- 11-06-2010, 01:04 AM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Where is the variable dblArray defined? If you create the variable before the switch statement you should be able to use it in any case.
- 11-06-2010, 01:28 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Here is my main so far. I put asterisks around where I defined dblArray, and where I set it to the parameter in case 4. I'm not really sure how to initialize it since the size of the array is set in case 1, and I can't figure out how to bring that value outside of case 1 to case 4(or case 2 for that matter).
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
**double[] dblArray;**
int index;
double y;
System.out.println("We need to create an array of doubles for you.");
System.out.print("How large should I make your array? ");
do{
try{
reader.nextInt();
System.out.println("Enter 1 for--Creating an array of doubles.");
System.out.println("Enter 2 for--Finding a specific value in array of doubles.");
System.out.println("Enter 3 for--Entering a specific value at a specific location in the array of doubles.");
System.out.println("Enter 4 for--Printing the double array.");
System.out.println("Enter 5 to exit.");
System.out.println();
System.out.print("What do you want to do? ");
switch (reader.nextInt()){
case 1: System.out.print("How large should I make your array? ");
int size = reader.nextInt();
dblArray = getDblArray(size);
break;
case 2: System.out.print("What double do you want to find? ");
reader.nextDouble();
**findInArray(dblArray);**
break;
case 3: System.out.print("What is the double you wish to put in the array? ");
reader.nextDouble();
System.out.print("Which index do you want replaced? ");
reader.nextInt();
case 4: **printArray(dblArray);**
break;
case 5: System.out.println("Bye");
System.exit(0);
default: System.out.println("Invalid integer.");
}
}catch (InputMismatchException inputMismatchException){
reader.nextLine();
System.out.println("Invalid response. Enter 1, 2, 3, 4, or 5.");
}
}while(true);
}
Sorry the spacing doesn't look right. It looks fine when I try to edit it, so I don't know how to fix it.Last edited by bossanova; 11-06-2010 at 01:32 AM.
- 11-06-2010, 01:37 AM #4
Member
- Join Date
- Oct 2010
- Location
- Fort Wayne, IN
- Posts
- 7
- Rep Power
- 0
It's very hard to answer your question without knowing exactly what getDblArray() and dblArray do. Is dblArray a variable? Is it another array of type double? By looking at your codes, it looks like dblArray is a method that initializes the array and getDblArray is a method that gets the size of the array from the user. If this is the case, then here is what you need to do:
public static void main(String[] args)
{
//local variables
int value = 0;
//put case 1 here
value = getDblArray();
//put case 4 here
printArray(dblArray(value));
}
public static double[] dblArray(int arraySize)
{
double[] array = new double[arraySize];
for(int index = 0; index < array.length; index++)
{
array[index] = index;
}
return array;
}
public static void printArray(double[] array)
{
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
public static int getDblArray()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the length of the array: ");
int numOfArray = scan.nextInt();
return numOfArray;
}
- 11-06-2010, 01:38 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,542
- Rep Power
- 11
Put [code] at the start of your code and [/code] at the end so that the formatting is preserved when you post.
Java Code:public static void main(String[] args) { Scanner reader = new Scanner(System.in); **double[] dblArray;** int index; double y; System.out.println("We need to create an array of doubles for you."); System.out.print("How large should I make your array? "); do{ try{ reader.nextInt(); System.out.println("Enter 1 for--Creating an array of doubles."); System.out.println("Enter 2 for--Finding a specific value in array of doubles."); System.out.println("Enter 3 for--Entering a specific value at a specific location in the array of doubles."); System.out.println("Enter 4 for--Printing the double array."); System.out.println("Enter 5 to exit."); System.out.println(); System.out.print("What do you want to do? "); switch (reader.nextInt()){ case 1: System.out.print("How large should I make your array? "); int size = reader.nextInt(); dblArray = getDblArray(size); break; case 2: System.out.print("What double do you want to find? "); reader.nextDouble(); **findInArray(dblArray);** break; case 3: System.out.print("What is the double you wish to put in the array? "); reader.nextDouble(); System.out.print("Which index do you want replaced? "); reader.nextInt(); case 4: printArray(dblArray); break; case 5: System.out.println("Bye"); System.exit(0); default: System.out.println("Invalid integer."); } }catch (InputMismatchException inputMismatchException){ reader.nextLine(); System.out.println("Invalid response. Enter 1, 2, 3, 4, or 5."); } }while(true); }
- 11-06-2010, 01:47 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Sorry for the confusion. dblArray is the actual array, while getDblArray(int len) is the method that creates the array with length len.
- 11-06-2010, 01:51 AM #7
Member
- Join Date
- Oct 2010
- Location
- Fort Wayne, IN
- Posts
- 7
- Rep Power
- 0
Java Code:public static void main(String[] args) { //local variables int value = 0; //put case 1 here value = getDblArray(); //put case 4 here printArray(dblArray(value)); } public static double[] dblArray(int arraySize) { double[] array = new double[arraySize]; for(int index = 0; index < array.length; index++) { array[index] = index; } return array; } public static void printArray(double[] array) { for(int i = 0; i < array.length; i++) { System.out.println(array[i]); } } public static int getDblArray() { Scanner scan = new Scanner(System.in); System.out.print("Enter the length of the array: "); int numOfArray = scan.nextInt(); return numOfArray; }
- 11-06-2010, 01:54 AM #8
Member
- Join Date
- Oct 2010
- Location
- Fort Wayne, IN
- Posts
- 7
- Rep Power
- 0
Look at all the methods that I just posted. They might give you an idea on how to wrtite your methods.
- 11-06-2010, 02:00 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,542
- Rep Power
- 11
I'm not sure I really understand this. Could you post the code you are using and describe the behaviour when you run it? (ie what options you choose etc and the output that results)How do I get dblArray in case 4 initialized so it recognizes getDblArray(size) as the array?
Java Code:double[] dblArray; //... do { try { //... switch (reader.nextInt()){ //... case 2: System.out.print("What double do you want to find? "); reader.nextDouble(); findInArray(dblArray);
Does this compile?
The example you post to illustrate the problem should be complete (but as minimal as you can make it) and should compile. If it does not and you can't understand the compiler's message, copy and post the entire message and indicate which line of your code it is referring to.Last edited by pbrockway2; 11-06-2010 at 02:04 AM.
- 11-06-2010, 02:43 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
Okay so I figured it out, I had to initialize the variable size outside of the case statement, but in the try. Thanks for the help. This is due tonight, so I may be asking for more help later:
Java Code:public static void main(String[] args) { Scanner reader = new Scanner(System.in); // use this to read values from the keyboard. int size; double[] dblArray; // you must use this array of doubles. int response; // use this variable to hold the menu number 1-5 that the user selects. int index; // use this to hold index numbers in menu items 2 and 3 double y; // use this to hold the double that the user wants to insert into array. menu item 3 System.out.println("We need to create an array of doubles for you."); System.out.print("How large should I make your array? "); do{ try{ size = reader.nextInt(); System.out.println("Enter 1 for--Creating an array of doubles."); System.out.println("Enter 2 for--Finding a specific value in array of doubles."); System.out.println("Enter 3 for--Entering a specific value at a specific location in the array of doubles."); System.out.println("Enter 4 for--Printing the double array."); System.out.println("Enter 5 to exit."); System.out.println(); System.out.print("What do you want to do? "); switch (reader.nextInt()){ case 1: System.out.print("How large should I make your array? "); dblArray = getDblArray(size); break; case 2: System.out.print("What double do you want to find? "); reader.nextDouble(); //findInArray(dblArray); break; case 3: System.out.print("What is the double you wish to put in the array? "); reader.nextDouble(); System.out.print("Which index do you want replaced? "); reader.nextInt(); case 4: dblArray = getDblArray(size); printArray(dblArray); break; case 5: System.out.println("Bye"); System.exit(0); default: System.out.println("Invalid integer."); } }catch (InputMismatchException inputMismatchException){ reader.nextLine(); System.out.println("Invalid response. Enter 1, 2, 3, 4, or 5."); } }while(true); }
Similar Threads
-
I don't understand: a=1;b=2;a=b;a=4 results in b=4 (in complex cases w/ iterator)
By gymshoe in forum New To JavaReplies: 3Last Post: 08-13-2009, 12:56 AM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks