Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2007, 06:03 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Read from console (Scanner Class)
Hi, today is the second day I am trying to get familiar with Java..I'm trying to implement reading from console.. When I use this Input class once, everything is ok, if I try to read to integers, I get exceptions and errors.. Please, help!
This is a main program
import java.util.Scanner;
import Input.Input;
class Min{
public static void main(String[] args){
int x=0,y=0,z=0, f=0, i=0;

x = Input.InputInt();
System.out.println(x);
y = Input.InputInt();
System.out.println(x);


}
}
and Input class itself
package Input;
import java.util.Scanner;
public class Input{
public static int InputInt(){
int x=0;
Scanner in = new Scanner(System.in);

//reads int from the console
//and stores into x

x=in.nextInt();
in.close();
return x;


}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-10-2007, 11:00 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You are getting that problem because InputInt() is static. That means that everything declared in that method is static, so it will be the same throughout execution. More on this: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

You are closing the Scanner stream in the InputInt()
Code:
in.close();
The problem is that once you close it, the stream is dead, so the next time you call it, it cant read from the command line (thats why it works on the first call of InputInt() but not the second.

One solution is to remove the in.close() and it will work fine. Another solution is the create an instance of the Input class and make InputInt non static:

Code:
import java.util.Scanner; class Min { public static void main(String[] args) { int x = 0, y = 0, z = 0, f = 0, i = 0; Input input = new Input(); x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x); } } class Input { public int InputInt() { int x = 0; Scanner in = new Scanner(System.in); // reads int from the console // and stores into x x = in.nextInt(); // notice that now you can close the stream, since the method is non static in.close(); return x; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-11-2007, 05:35 AM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Quote:
Originally Posted by staykovmarin View Post
You are getting that problem because InputInt() is static. That means that everything declared in that method is static, so it will be the same throughout execution. More on this: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

You are closing the Scanner stream in the InputInt()
Code:
in.close();
The problem is that once you close it, the stream is dead, so the next time you call it, it cant read from the command line (thats why it works on the first call of InputInt() but not the second.

One solution is to remove the in.close() and it will work fine. Another solution is the create an instance of the Input class and make InputInt non static:

Code:
import java.util.Scanner; class Min { public static void main(String[] args) { int x = 0, y = 0, z = 0, f = 0, i = 0; Input input = new Input(); x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x); } } class Input { public int InputInt() { int x = 0; Scanner in = new Scanner(System.in); // reads int from the console // and stores into x x = in.nextInt(); // notice that now you can close the stream, since the method is non static in.close(); return x; } }
Thanks a lot. I just removed in.close(), and it worked, on the other hand it doesn't work, when I make non-static (I just copied your code)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-11-2007, 05:48 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Define doesnt work:
Code:
x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x);
You are also printing x twice there.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-11-2007, 05:59 AM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Quote:
Originally Posted by staykovmarin View Post
Define doesnt work:
Code:
x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x);
You are also printing x twice there.
Yes, I saw that))

Exception in Thread "main" java.util.NoSuchElementException
at ..
at ..
.. smth like that
after I enter the first value, it prints it and right after that I get that exception
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-11-2007, 06:08 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Post your current code (also a partial exception is really of no help).

I have a feeling that InputInt is still static.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-11-2007, 05:13 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Quote:
Originally Posted by staykovmarin View Post
Post your current code (also a partial exception is really of no help).

I have a feeling that InputInt is still static.
import java.util.Scanner;

class Input {
public int InputInt() {
int x = 0;
Scanner in = new Scanner(System.in);

// reads int from the console
// and stores into x
x = in.nextInt();

// notice that now you can close the stream, since the method is non static
in.close();

return x;
}

}
___________________

import java.util.Scanner;

class Input {
public int InputInt() {
int x = 0;
Scanner in = new Scanner(System.in);

// reads int from the console
// and stores into x
x = in.nextInt();

// notice that now you can close the stream, since the method is non static
in.close();

return x;
}

}



PS: how do you separate code from a message?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-11-2007, 10:48 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Post your Min class too?

Use the code tags: [ code][/ code]
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-11-2007, 10:54 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Code:
import java.util.Scanner; class Min { public static void main(String[] args) { int x = 0, y = 0, z = 0, f = 0, i = 0; Input input = new Input(); x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(y); } }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-11-2007, 11:06 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
import java.util.Scanner; class Min { public static void main(String[] args) { int x = 0, y = 0, z = 0, f = 0, i = 0; Input input = new Input(); x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(y); input.close(); } } class Input { Scanner in; public int InputInt() { int x = 0; in = new Scanner(System.in); // reads int from the console // and stores into x x = in.nextInt(); return x; } public void close() { in.close(); } }
So after a little bit of reading, i found out that after you close a stream, it cant be reopened, even if you create a new one (although i swear that it worked fine when i did it :\ ) So here is another solution. Just close the stream after you are done getting the numbers.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-11-2007, 11:11 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Quote:
Originally Posted by staykovmarin View Post
Code:
import java.util.Scanner; class Min { public static void main(String[] args) { int x = 0, y = 0, z = 0, f = 0, i = 0; Input input = new Input(); x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(y); input.close(); } } class Input { Scanner in; public int InputInt() { int x = 0; in = new Scanner(System.in); // reads int from the console // and stores into x x = in.nextInt(); return x; } public void close() { in.close(); } }
So after a little bit of reading, i found out that after you close a stream, it cant be reopened, even if you create a new one (although i swear that it worked fine when i did it :\ ) So here is another solution. Just close the stream after you are done getting the numbers.
But in this case, I cannot use that package Input as an independable.. i mean, I have to close the thread from the main program .. is that correct?
thnx
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read input from console Java Tip java.io 0 04-16-2008 11:57 PM
How to read input from the console Java Tip java.io 0 04-06-2008 08:41 PM
Reading a line from console using Scanner class Java Tip Java Tips 0 01-18-2008 12:52 PM
Using Scanner class to read int Java Tip Java Tips 0 01-18-2008 12:50 PM
Using Scanner class to read int value Java Tip Java Tips 0 12-03-2007 10:34 AM


All times are GMT +3. The time now is 09:45 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org