Results 1 to 5 of 5
  1. #1
    tripke is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,523
    Rep Power
    5

    Default 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 07:06 PM. Reason: silly grammar
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    tripke is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,523
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    tripke is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

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

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

Similar Threads

  1. A C programmer
    By xperia2995 in forum New To Java
    Replies: 3
    Last Post: 12-28-2012, 01:42 PM
  2. Replies: 32
    Last Post: 02-29-2012, 09:21 AM
  3. !!! Java Programmer !!!
    By meili in forum Forum Lobby
    Replies: 2
    Last Post: 08-11-2009, 03:12 PM
  4. Becoming a better programmer.
    By AnGuRuSO in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2008, 04:39 PM
  5. Programmer Gone Bad
    By sixohseven in forum Introductions
    Replies: 2
    Last Post: 08-09-2007, 06:18 PM

Posting Permissions

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