Results 1 to 11 of 11
- 12-10-2007, 06: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, 11: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()
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, 05:35 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
- 12-11-2007, 05:48 AM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Define doesnt work:
Java Code:x = input.InputInt(); System.out.println(x); y = input.InputInt(); System.out.println(x);
- 12-11-2007, 05:59 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
- 12-11-2007, 06: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, 05: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, 10: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, 10: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, 11:06 PM #10
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- 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); 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, 11:11 PM #11
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
How to read input from console
By Java Tip in forum java.ioReplies: 0Last Post: 04-16-2008, 11:57 PM -
How to read input from the console
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 08:41 PM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 12:52 PM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 12:50 PM -
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 10:34 AM
Bookmarks