-1

I am getting an error on the 'start' in the (start + finish) at the bottom. The only way to fix it is to add "int start = inputFn.nextInt();" below ' Scanner inputFn = new Scanner(System.in);' but that screws it all up. I was able to fix it before but I forgot how I did it. Please help.

public static void main(String eth[]){

System.out.println("Please Enter An Integer:");
Scanner inputFn = new Scanner(System.in);


if (inputFn.hasNextInt()){
}else{
int start = inputFn.nextInt();
System.out.print("Play By The Rules And Enter An Integer");

}

System.out.println("Please Enter An Integer:");
Scanner inputSn = new Scanner(System.in);
int finish = inputSn.nextInt();

int answer = (start + finish);
System.out.println(answer);


}
}
  • 2
    **Be specific.** What error are you getting? – Makoto Apr 26 '15 at 01:15
  • You need to declare the variable (`start` in your case) outside of the else, because it is only visible inside the declared scope. Alternatively you can put all of the second read into the else. – eckes Apr 26 '15 at 01:16
  • You're declaring the `start` variable in a condition-scope and are trying to call it at method-scope. This isn't possible. Better indentation of your code would better show this as being the problem you're having. – Drew Kennedy Apr 26 '15 at 01:17
  • learn to format your code in a sane manner, you can only expect the same amount of effort you put into asking the question from someone helping, right that that is not much ... –  Apr 26 '15 at 01:19

1 Answers1

0

The if

if (inputFn.hasNextInt()){
}else{
  int start = inputFn.nextInt();
  System.out.print("Play By The Rules And Enter An Integer");
}

is kind of backwards. You are reading the integer only if there ISN'T one. And you are defining the start variable inside the if block which is why you are getting the error.

Something more like

int start = 0;
if (inputFn.hasNextInt()){
  start = inputFn.nextInt();
}else{
  System.out.print("Play By The Rules And Enter An Integer");
}

would seem better.

eckes
  • 9,350
  • 1
  • 52
  • 65
Evan Williams
  • 396
  • 2
  • 8