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
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!
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 -> :smash:
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();
}
}
}
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?
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. :(
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
Re: Can't get this simple Calculator app to work!
My problem is a resource leak. sc is never closed.
Re: Can't get this simple Calculator app to work!
Quote:
Originally Posted by
postauma
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