Skip to main content



      Home
Home » Newcomers » Newcomers » Any help plz(Having trouble running program)
icon4.gif  Any help plz [message #896267] Tue, 17 July 2012 15:50 Go to next message
Eclipse UserFriend
I am not understanding the error. I wrote this straight out of my Java book by Tony Gaddis. I am using eclipse "juno" and not sure what i am doing wrong. The error i get is called, Resource leak: "keyboard" is never closed. I have not adjusted any preferences. Any help would be appreciated, thank you.


import java.util.Scanner;

public class Payroll
{
public static void main(String[]args)
{
String name; // To hold a name.
int hours; // Hours worked.
double payRate; // Hourly pay rate.
double grossPay; // Gross pay.


/// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);

// Get the user's name.
System.out.print("What is your name?");
name = keyboard.nextLine();

// Get the number of hours worked this week.
System.out.print("How many hours did you work this week? ");
hours = keyboard.nextInt();

// Get the user's hourly pay rate.
System.out.print("What is your hourly pay rate? ");
payRate = keyboard.nextDouble();

// Calculate the gross pay.
grossPay = hours * payRate;

// Display the resulting information.
System.out.println("Hello " + name);
System.out.println("Your gross pay is $" + grossPay);


}
}
Re: Any help plz [message #896270 is a reply to message #896267] Tue, 17 July 2012 15:59 Go to previous messageGo to next message
Eclipse UserFriend
If I recall correctly, Scanner objects must be closed when they are no longer needed, so at the end of your method (before the two closing brackets), you need to add the line:

keyboard.close();
Re: Any help plz [message #896274 is a reply to message #896270] Tue, 17 July 2012 16:18 Go to previous messageGo to next message
Eclipse UserFriend
wow, that was easy fix. But it's weird because that code was straight out the book, minus that fix and it was suppose to compile. Anyways thank you.
Re: Any help plz [message #896276 is a reply to message #896274] Tue, 17 July 2012 16:27 Go to previous messageGo to next message
Eclipse UserFriend
Books aren't always 100% correct, though it is surprising that something as easy to check would be overlooked, especially on such a simple program. In any case, I'm glad it's working for you now!
Re: Any help plz [message #897298 is a reply to message #896274] Mon, 23 July 2012 10:10 Go to previous messageGo to next message
Eclipse UserFriend
On 17.07.2012 22:18, Clayton Pounds wrote:
> wow, that was easy fix. But it's weird because that code was straight
> out the book, minus that fix and it was suppose to compile. Anyways
> thank you.
Eclipse reports detects more problems than 'javac'. Note that Eclipse
still compiled the code (i.e. generated the class file).

Dani
Re: Any help plz [message #1059620 is a reply to message #896276] Mon, 20 May 2013 17:03 Go to previous messageGo to next message
Eclipse UserFriend
Hej,
i have a similar problem.
I think (don't know) that there is something wrong.
So the scanner problem seems to point at a problem in eclipse.

In my case Eclipse give the warning that the scanner has not been close, however if i add the 'scanner.close()' function it dont do what it suppose to.
I'm a beginner but the code seems to be right.
I normaly dont use eclipse and i know that the code works perfectly in other programs,
but not in eclipse. I dont meen the warnings, in eclipse the code just crashes and i would like to know why?

so this is the code that do not work in eclipse:


import java.util.Scanner;

public class Scan {

public static int reader(){
int x = 0;

do{
try
{
System.out.print("[E]:");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
sc.close();
}
catch(Exception ex){ System.out.println(ex);x=-1; }
}while(x<0);
System.out.println("[ok]");
return x;
}

}

If i remove the close() out of the code it works fine with the exception of warnings.

Re: Any help plz [message #1059769 is a reply to message #1059620] Tue, 21 May 2013 10:35 Go to previous messageGo to next message
Eclipse UserFriend
On 5/21/2013 6:47 AM, Pawel BobTosh wrote:
> Hej,
> i have a similar problem.
> I think (don't know) that there is something wrong. So the scanner
> problem seems to point at a problem in eclipse.
>
> In my case Eclipse give the warning that the scanner has not been close,
> however if i add the 'scanner.close()' function it dont do what it
> suppose to.
> I'm a beginner but the code seems to be right.
> I normaly dont use eclipse and i know that the code works perfectly in
> other programs,
> but not in eclipse. I dont meen the warnings, in eclipse the code just
> crashes and i would like to know why?
>
> so this is the code that do not work in eclipse:
>
>
> import java.util.Scanner;
>
> public class Scan {
>
> public static int reader(){
> int x = 0;
>
> do{
> try
> {
> System.out.print("[E]:");
> Scanner sc = new Scanner(System.in);
> x = sc.nextInt();
> sc.close();
> }
> catch(Exception ex){ System.out.println(ex);x=-1; }
> }while(x<0);
> System.out.println("[ok]");
> return x;
> }
>
> }
>
> If i remove the close() out of the code it works fine with the exception
> of warnings.

If, by "doesn't work in Eclipse", you mean that you can't use the
Console view to type in input, this is true. This program will only work
correctly run outside of Eclipse. The Eclipse console window doesn't
support input.
Re: Any help plz [message #1150285 is a reply to message #896270] Tue, 22 October 2013 13:15 Go to previous messageGo to next message
Eclipse UserFriend
Very Happy

Thank you so much!
That the exactly same issue i have with the Scanner too...
Re: Any help plz [message #1573661 is a reply to message #896270] Mon, 19 January 2015 16:51 Go to previous message
Eclipse UserFriend
can you put an example of what you are talking about with the " at the end of your method (before the two closing brackets), you need to add the line:"

I am totally confused and the word keyboard doesn't show up in chapter 3 in the book"Introduction to Java Programming 9th edition". Or am I not seeing it.

does that keyboard word go when I initiate the Scanner

like right here
// Create a Scanner
Scanner input = new Scanner(System.in);

or you mean these closing brackets
public class AdditionQuiz {
public static void main(String[] args) {

I am so confused I didn't have that much trouble in VS.
Previous Topic:Displaying .rptdesign file on browser
Next Topic:Java Jersey PUT-Method and Client
Goto Forum:
  


Current Time: Tue Jul 22 17:44:09 EDT 2025

Powered by FUDForum. Page generated in 0.09379 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top