# Thread: New Programmer--Could use some help with this bit of code :)

1. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## New Programmer--Could use some help with this bit of code :)

Hello everyone,

I am new to java programming, but am taking huge strides in learning it. So far I have been watching a few video series to get myself started and have been playing around with it myself, but I have an error in my code that I am not sure how to fix. I am using eclipse to write my code in.

Java Code:
```import java.util.Scanner;

class tuna{
public static void main(String args[]){
System.out.println("Hello, this application allows you to either find the Area of a Circle or use it as a Basic Calculator");
System.out.println("Enter 1 for Area of a Circle.");
System.out.println("Enter 2 for a Basic Calculator.");
Scanner Taylor = new Scanner(System.in);
double Question = Taylor.nextDouble();
double Answer = 1;
if (Question == Answer){
Scanner Area = new Scanner(System.in);
double Pi = 3.14;
System.out.println("Enter the radius of the circle");
double radius = Area.nextDouble();
double area = radius * radius * Pi;
double circumference = 2 * radius * Pi;
System.out.println("The area of the circle is " + area);
System.out.println("The circumference of the circle is " + circumference);
}else{
Scanner Calculator = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number.");
fnum = Calculator.nextDouble();
System.out.println("Enter second number.");
snum = Calculator.nextDouble();
answer = fnum + snum;
System.out.println("Answer:");
System.out.println(answer);

Calculator.close();
Taylor.close();

}

}
}```
Near the bottom I have a Calculator.close(); and a Taylor.close();, but I need to close the Area Scanner. When I do this though it gives me an error saying that Area is not used or something that like. Any help would be appreciated! :)

2. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,735
Rep Power
4

## Re: New Programmer--Could use some help with this bit of code :)

Your Area scanner is a local variable and its scope of visibility is constrained to the first part of the if-else construct. However, you don't need multiple scanners to read input from the console. One should be sufficient. Just use that for general input and close it at the end.

Regards,
Jim
Last edited by jim829; 02-19-2013 at 08:06 PM. Reason: silly grammar

3. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: New Programmer--Could use some help with this bit of code :)

Okay, I believe I understand what you are saying. How then could I use one scanner in the entire code? When I replace the Area scanner with my Taylor one at the top, it says there are duplicate local variables. Sorry for my lack of understanding, but how do I use just one scanner for the entire piece of code?

4. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,735
Rep Power
4

## Re: New Programmer--Could use some help with this bit of code :)

Something like this

Java Code:
```Scanner input = new Scanner(System.in);
...

double radius = input.nextDouble();
...

int arg1 = input.nextInt();  // or whatever type you want
int arg2 = input.nextInt();

int sum = arg1 + arg2;```
Note: You may need to do a input.nextLine() after each numeric input so you can flush the line termination characters out of the input buffer. System.in.flush() may also work but I have not tried it.

Jim

5. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: New Programmer--Could use some help with this bit of code :)

Thank you Jim. I was able to get it to work :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•