|
|
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.
|
|

12-10-2007, 07:03 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
|
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;
}
}
|
|

12-11-2007, 12:00 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
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()
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:
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;
}
}
|
|

12-11-2007, 06:35 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
Originally Posted by staykovmarin
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()
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:
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)
|
|

12-11-2007, 06:48 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
Define doesnt work:
x = input.InputInt();
System.out.println(x);
y = input.InputInt();
System.out.println(x);
You are also printing x twice there.
|
|

12-11-2007, 06:59 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
Originally Posted by staykovmarin
Define doesnt work:
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
|
|

12-11-2007, 07:08 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
|
Post your current code (also a partial exception is really of no help).
I have a feeling that InputInt is still static.
|
|

12-11-2007, 06:13 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
Originally Posted by staykovmarin
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? 
|
|

12-11-2007, 11:48 PM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
|
Post your Min class too?
Use the code tags: [ code][/ code]
|
|

12-11-2007, 11:54 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
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);
}
}
|
|

12-12-2007, 12:06 AM
|
|
Senior Member
|
|
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
|
|
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.
|
|

12-12-2007, 12:11 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 21
|
|
Originally Posted by staykovmarin
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|