Results 1 to 10 of 10
  1. #1
    bossanova is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    306
    Rep Power
    6

    Default

    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.

  3. #3
    bossanova is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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.

  4. #4
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    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;

    }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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);
    }

  6. #6
    bossanova is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry for the confusion. dblArray is the actual array, while getDblArray(int len) is the method that creates the array with length len.

  7. #7
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    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;
    		
    	}

  8. #8
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    Look at all the methods that I just posted. They might give you an idea on how to wrtite your methods.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How do I get dblArray in case 4 initialized so it recognizes getDblArray(size) as the array?
    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)

    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.

  10. #10
    bossanova is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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

  1. Replies: 3
    Last Post: 08-13-2009, 12:56 AM
  2. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  4. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •