Search code examples
javadrjava

Sorting Arrays, duplicate local variable, error


I am working on this code all day, and I just can't seem to get it right :(

import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class TrialSixthree{
        public static void main(String[]args){
           Scanner i = new Scanner(System.in);

           int c;
           int choice;
           Random t = new Random();
            for (c = 1; c <= 5; c++) {
                System.out.println(t.nextInt(1000));
            }
            System.out.println(" \n1: BUBBLE SORT ");
            System.out.println(" 2: SELECTION SORT ");
            System.out.println(" 3: QUICK SORT ");
            System.out.println(" Choose a number from 1-3 ");
            choice= i.nextInt();

            if(choice == 1){
                System.out.print("You chose BUBBLE sort!");
                System.out.println(x[i]+"");//I don't know what's wrong in this line
                for(int i = 0 ; i < x.length-1 ; i++){
                  for (int j = 0 ; j < x.length-1 ; j++){
                    if ( x[j] > x[j]){
                        temp = x[j];
                        x[j] = x[j+1];
                        x[j+1] = temp;
                    }
                  }
                }
              } else if(choice == 2){
                  System.out.print("You chose SELECTION sort!");
                  System.out.println(x[i]+"");
                  int temp, min;
                  for(int i=0;i<x.length-1;i++) {
                      min = i;
                      for(int j=i+1;j<x.length;j++) {
                        if(x[min]>x[j]) {
                            min = j;
                        }
                        if(min!=i) {
                            temp = x[min];
                            x[min] = x[i];
                            x[i] = temp;
                        }
                      }
                    }
                  } else if(choice == 3){
                        System.out.println("You chose QUICK sort!");
                        System.out.println(x[i]+"");
                        int temp;
                        for(int i=0;i<x.length-1;i++) {
                            for(int j=i+1;j<x.length;j++) {
                              if(x[i]>x[j]) {
                                  temp = x[i];
                                  x[i] = x[j];
                                  x[j] = temp;
                              }
                            }
                        }
                    } else {
                          System.out.println("Not in the choices!");
                    }
                }
            }

I just can't seem to get this right. I'm still new to java. There's an error that says duplicate local variable. It's my homework. Please help me :(


Solution

  • You declared i twice :

    Scanner i = new Scanner(System.in);
    

    and

    for(int i = 0 ; i < x.length-1 ; i++)
    

    I suggest you give the Scanner variable a more meaningful name.