How can I simplify my sorting class and user input for my TestStudent program?

In summary, The conversation is about the issues with a sorting class called from the main program TestStudent. The user's input is not being read correctly into the array and the code appears to be more complicated than necessary with duplicate cases and response variables. The suggestion is to simplify the switch statement and use only one response variable for easier troubleshooting.
  • #1
major_maths
30
0
I'm having issues with a sorting class that I'm trying to call from my main program, TestStudent. I don't think it's taking the user's input correctly when I try to read it into an array but I can't figure out why.

class Student:
Code:
public static void selectionSort(double[] numbers)
    {              
        int min_index;
        double temp;
        for(int i=0; i<numbers.length-1; i++)
        {
            min_index = i;
            for(int j=min_index+1; j<numbers.length; j++)
            {
                if (numbers[j]<numbers[min_index])
                {min_index=j;}
            }
            temp = numbers[min_index];
            numbers[min_index] = numbers[i];
            numbers[i] = temp;
        }
    }

TestStudent:
Code:
System.out.println("Would you like to sort students' scores according to average?");
       String response3 = keyboard.next();
       char letter3 = response3.charAt(0);
       String response4 = "n";
       char letter4 = response4.charAt(0);
       
       double[] number = new double[10];
       do{
           switch (letter3)
           {
               case 'y':
                    System.out.println("Great! Please enter the averages to be sorted.");
                    for(int m=0; m<number.length; m++)
                    {number[m] = keyboard.nextDouble();}

                    Student.selectionSort(number);

                    for(int l=0; l<number.length; l++)
                    {System.out.println(number[l]+" ");}
                    
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                case 'Y':
                    System.out.println("Cool! Please enter the averages to be sorted.");
                    for(int m=0; m<number.length; m++)
                    {number[m] = keyboard.nextDouble();}
                    
                    Student.selectionSort(number);
                    
                    for(int l=0; l<number.length; l++)
                    {System.out.println(number[l]+" ");}
                    
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                case 'n':
                    break;
                case 'N':
                    break;
                default:
                    System.out.println("Could not recognize answer.");
            }
        }while(response3.equalsIgnoreCase("y")&&response4.equalsIgnoreCase("y"));
 
Technology news on Phys.org
  • #2
major_maths said:
I'm having issues with a sorting class that I'm trying to call from my main program, TestStudent. I don't think it's taking the user's input correctly when I try to read it into an array but I can't figure out why.

class Student:
Code:
public static void selectionSort(double[] numbers)
    {              
        int min_index;
        double temp;
        for(int i=0; i<numbers.length-1; i++)
        {
            min_index = i;
            for(int j=min_index+1; j<numbers.length; j++)
            {
                if (numbers[j]<numbers[min_index])
                {min_index=j;}
            }
            temp = numbers[min_index];
            numbers[min_index] = numbers[i];
            numbers[i] = temp;
        }
    }

TestStudent:
Code:
System.out.println("Would you like to sort students' scores according to average?");
       String response3 = keyboard.next();
       char letter3 = response3.charAt(0);
       String response4 = "n";
       char letter4 = response4.charAt(0);
       
       double[] number = new double[10];
       do{
           switch (letter3)
           {
               case 'y':
                    System.out.println("Great! Please enter the averages to be sorted.");
                    for(int m=0; m<number.length; m++)
                    {number[m] = keyboard.nextDouble();}

                    Student.selectionSort(number);

                    for(int l=0; l<number.length; l++)
                    {System.out.println(number[l]+" ");}
                    
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                case 'Y':
                    System.out.println("Cool! Please enter the averages to be sorted.");
                    for(int m=0; m<number.length; m++)
                    {number[m] = keyboard.nextDouble();}
                    
                    Student.selectionSort(number);
                    
                    for(int l=0; l<number.length; l++)
                    {System.out.println(number[l]+" ");}
                    
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                case 'n':
                    break;
                case 'N':
                    break;
                default:
                    System.out.println("Could not recognize answer.");
            }
        }while(response3.equalsIgnoreCase("y")&&response4.equalsIgnoreCase("y"));

Your code is more complicated than it needs to be, with duplicate cases for 'Y' and 'y' and with two response variables.

You can simplify your switch statement like so:
Code:
           switch (letter3)
           {
               case 'y':
               case 'Y':
                    System.out.println("Cool! Please enter the averages to be sorted.");
                    for(int m=0; m<number.length; m++)
                    {number[m] = keyboard.nextDouble();}
                    
                    Student.selectionSort(number);
                    
                    for(int l=0; l<number.length; l++)
                    {System.out.println(number[l]+" ");}
                    
                    System.out.println("Would you like to sort more student's scores? Please type 'y' or 'n'.");
                    response4 = keyboard.next();
                    break;
                default:
                    System.out.println("Could not recognize answer.");
            }

If the value of letter3 is 'y' execution falls through to the 'Y' case. If the value of letter3 is 'Y', the first case is skipped and execution flows to the 'Y' case.

I removed the 'n' and 'N' cases, since you weren't doing anything with them.

What you should do is eliminate one of your response variables - I don't see the need for two of them. With just one, you can simplify the logic of your do - while loop, and are more likely to figure out what's working and what isn't.
 

1. What is an array?

An array is a data structure that can hold a collection of elements of the same data type. It is used to store and organize data in a sequential manner, making it easier to access and manipulate the elements.

2. What is the difference between an array and a loop?

An array is a data structure, while a loop is a programming construct used to iterate through a set of instructions or data. Arrays can be used in conjunction with loops to perform repetitive tasks on the elements within the array.

3. How do you declare an array?

An array can be declared in most programming languages using the syntax: data_type array_name[size]; where data_type is the type of data the array will hold, array_name is the name given to the array, and size is the number of elements the array can hold.

4. What is a loop counter?

A loop counter is a variable used to keep track of the number of iterations a loop has performed. It is commonly used in for loops, where it is initialized at the beginning of the loop and incremented or decremented with each iteration.

5. How do you use a loop to access elements in an array?

There are different types of loops that can be used to access elements in an array, such as for loops, while loops, and do-while loops. Typically, a loop counter is used to iterate through the array and access each element using its index within the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top