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

    Unhappy Can't get this simple Calculator app to work!

    I'm writing a java application and don't know why my code isn't working. I'm getting an error because the scanner sc is not closed. I don't know if that's my only error or how to fix it and have looked through google and my book. Here's my code if anyone can take a look at it. I need this program working before midnight!

    Help!

    Code: Divisor Calculator - Pastebin.com

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get this simple Calculator app to work!

    Hello postauma and welcome to the java-forums.org!

    If you have a question about your code, please post that code here in the forum so all can easily see it and evaluate it. Don't forget to use code tags around your posted code too so that it retains its formatting. The link in my signature below will show you how to do this. Good luck, and again welcome!

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

    Default Re: Can't get this simple Calculator app to work!

    Thanks for the warm welcome Fuba, and thanks for the advice. Here's my code. I really hope someone can help. I'm starting to feel like this guy ->

    Java Code:
        import java.util.Scanner;
         
        public class DivisorCalc {
         
                /**
                 * @param args
                 */
                public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        Scanner sc = new Scanner(System.in);
                        String choice = "y";           
                        while (!choice.equalsIgnoreCase ("y")){
                                 System.out.print("Please enter the first number: ");
                                 int x = sc.nextInt();
                                 System.out.println("Please enter the second number: ");
                                 int y = sc.nextInt();
                                 
                                 do{
                                         do{
                                                 int tempY = (y-x);
                                                 y = tempY;
                                                                 
                                         }
                                         while (y>x);
                                         
                                         int tempX = x;
                                         x = y;
                                         y = tempX;
                                 }
                                 while (x != 0);
                                 
                                 String message =
                                                 "Greatest common divisor: " + y + "\n"
                                                 + "";
                                 System.out.println(message);
                                 
                                 System.out.println("Continue? (y/n): ");
                                 choice = sc.next();
                                 System.out.println();
                        }
                        }
         
                }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get this simple Calculator app to work!

    Thanks for posting the code. Now are you seeing a compiler error message or a warning? Can you post this message in its entirety and also let us know which line of your code it refers to?

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

    Default Re: Can't get this simple Calculator app to work!

    I'm getting this warning at line 10:

    Multiple markers at this line
    -Line breakpoint:DivisorCalc [line:10] - main(String[])
    -Resource leak: 'sc' is never closed

    I don't see why that's preventing me from running the program how it is though. :(

  6. #6
    postauma is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Can't get this simple Calculator app to work!

    Description Resource Path Location Type
    Resource leak: 'sc' is never closed DivisorCalc.java /DivisorCalculator/src line 10 Java Problem

  7. #7
    postauma is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Can't get this simple Calculator app to work!

    My problem is a resource leak. sc is never closed.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't get this simple Calculator app to work!

    Quote Originally Posted by postauma View Post
    My problem is a resource leak. sc is never closed.
    So close it at the end of your program; b.t.w. I've never seen that error/warning message. Is it a compiler message or a (debug) runtime message?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why doesn't my javascript calculator work?
    By twinbird24 in forum New To Java
    Replies: 2
    Last Post: 03-29-2012, 05:55 AM
  2. Can't get java calculator to work
    By name in forum New To Java
    Replies: 4
    Last Post: 12-02-2011, 03:38 PM
  3. actions dont work properly, calculator
    By lordjb in forum New To Java
    Replies: 14
    Last Post: 02-03-2011, 07:00 AM
  4. Simple Calculator
    By marksey07 in forum New To Java
    Replies: 12
    Last Post: 01-09-2011, 06:30 AM
  5. Getting Backspace on a java calculator program to work
    By marylanddem in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 01:01 AM

Tags for this Thread

Posting Permissions

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