-1

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 :(

WonderWorld
  • 956
  • 1
  • 8
  • 18
  • Well, both your `Scanner` and a loop variable are called `i`, and your variable `x` is never declared at all. Read the error messages, they are almost always very helpful. – Keppil Mar 07 '15 at 13:27

2 Answers2

1

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Also i don't see the `x` array declared anywhere. And an `else if` statement without an `if` - `else if(choice == 3)` it comes after the `for` loop because of missing closing brace of the nested for loop. – WonderWorld Mar 07 '15 at 13:42
0

You are used a i variable in multiple time So rename a i variable

       Scanner i=new Scanner(System.in);

Rename a i varible

       Scanner scan=new Scanner(System.in);  
Benjamin
  • 2,257
  • 1
  • 15
  • 24