0

Everything else is working in both programs but every time I try to compile and run it gives me this error Duplicate local variable input and I can not seem to fix it. I've looked up solutions none seem to work.

**Scanner scan = new Scanner(System.in); **//Duplicate****
      
System.out.println("What is the capital of Canada?");
      
String input = scan.nextLine();

if (input.equalsIgnoreCase("Ottawa")) {
    System.out.println("Correct");
} else {
    System.out.println("incorrect");
}

Seperate Program

I have tested this code out on a test run and everything runs smoothly when it is not joined with the code posted ahead of this one and I need it for an assignment due tonight.

**Scanner input = new Scanner(System.in); //This is the error. Also Duplicate.**
      
int num1;
int num2;
int num3;
int sum;
      
System.out.println("Enter first integer:");
num1 = input.nextInt();
      
System.out.println("Enter second integer:");
num2 = input.nextInt();
      
System.out.println("Enter third integer:");
num3 = input.nextInt();
      
sum = num1 + num2 + num3;
      
if (num1 > num2 & num2 > num3) {
    System.out.println("The number is");
 
    System.out.println(sum); 
}
Community
  • 1
  • 1
k wets
  • 1
  • 1

2 Answers2

0

When you join them together, instead of creating a new Scanner object:

Scanner input = new Scanner(System.in);

just do:

input = new Scanner(System.in);
Oladipo
  • 1,579
  • 3
  • 17
  • 33
0
**Scanner scan = new Scanner(System.in); **//Duplicate****

System.out.println("What is the capital of Canada?");

String input = scan.nextLine();

if (input.equalsIgnoreCase("Ottawa")) {
    System.out.println("Correct");
} else {
    System.out.println("incorrect");
}

**Scanner input2 = new Scanner(System.in); //This is the error. Also Duplicate.**

int num1;
int num2;
int num3;
int sum;

System.out.println("Enter first integer:");
num1 = input2.nextInt();

System.out.println("Enter second integer:");
num2 = input2.nextInt();

System.out.println("Enter third integer:");
num3 = input2.nextInt();

sum = num1 + num2 + num3;

if (num1 > num2 & num2 > num3) {
    System.out.println("The number is");

    System.out.println(sum); 
}
k wets
  • 1
  • 1