Results 1 to 11 of 11
- 12-10-2007, 05:03 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
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-10-2007, 10:00 PM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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.Java Code:in.close();
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:
Java 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; } }
- 12-11-2007, 04:35 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
- 12-11-2007, 04:48 AM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Define doesnt work:
You are also printing x twice there.Java Code:x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x);
- 12-11-2007, 04:59 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
- 12-11-2007, 05:08 AM #6
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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, 04:13 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
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? :o
- 12-11-2007, 09:48 PM #8
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Post your Min class too?
Use the code tags: [ code][/ code]
- 12-11-2007, 09:54 PM #9
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
Java 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); } }
- 12-11-2007, 10:06 PM #10
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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.Java 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(); } }
- 12-11-2007, 10:11 PM #11
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
How to read input from console
By Java Tip in forum java.ioReplies: 0Last Post: 04-16-2008, 10:57 PM -
How to read input from the console
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:41 PM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks