Results 1 to 5 of 5
- 02-19-2013, 06:52 PM #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.
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! :)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(); } } }
- 02-19-2013, 07:05 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 657
- Rep Power
- 1
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,
JimLast edited by jim829; 02-19-2013 at 07:06 PM. Reason: silly grammar
The Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 07:11 PM #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?
- 02-19-2013, 07:27 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 657
- Rep Power
- 1
Re: New Programmer--Could use some help with this bit of code :)
Something like this
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.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;
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-19-2013, 07:56 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
A C programmer
By xperia2995 in forum New To JavaReplies: 3Last Post: 12-28-2012, 01:42 PM -
What's wrong with my code? (Danish java-programmer would be greatly appriciated!)
By spande in forum New To JavaReplies: 32Last Post: 02-29-2012, 09:21 AM -
!!! Java Programmer !!!
By meili in forum Forum LobbyReplies: 2Last Post: 08-11-2009, 03:12 PM -
Becoming a better programmer.
By AnGuRuSO in forum Advanced JavaReplies: 1Last Post: 11-21-2008, 04:39 PM -
Programmer Gone Bad
By sixohseven in forum IntroductionsReplies: 2Last Post: 08-09-2007, 06:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks