Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Any help plz(Having trouble running program)
icon4.gif  Any help plz [message #896267] Tue, 17 July 2012 19:50 Go to next message
Clayton Pounds is currently offline Clayton PoundsFriend
Messages: 2
Registered: July 2012
Junior Member
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 19:59 Go to previous messageGo to next message
Jason Bury is currently offline Jason BuryFriend
Messages: 5
Registered: July 2012
Junior Member
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 20:18 Go to previous messageGo to next message
Clayton Pounds is currently offline Clayton PoundsFriend
Messages: 2
Registered: July 2012
Junior Member
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 20:27 Go to previous messageGo to next message
Jason Bury is currently offline Jason BuryFriend
Messages: 5
Registered: July 2012
Junior Member
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 14:10 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
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 21:03 Go to previous messageGo to next message
Pawel BobTosh is currently offline Pawel BobToshFriend
Messages: 1
Registered: May 2013
Junior Member
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 14:35 Go to previous messageGo to next message
Russell Bateman is currently offline Russell BatemanFriend
Messages: 3798
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

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 17:15 Go to previous messageGo to next message
amy krizan is currently offline amy krizanFriend
Messages: 1
Registered: October 2013
Junior Member
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 21:51 Go to previous message
Michele Kinsella is currently offline Michele KinsellaFriend
Messages: 2
Registered: January 2015
Junior Member
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: Thu Apr 18 03:57:07 GMT 2024

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

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

Back to the top