Results 1 to 11 of 11
  1. #1
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default 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;


    }

    }

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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();
    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:

    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;
    	}
    
    }

  3. #3
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    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()
    Java 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:

    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;
    	}
    
    }
    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)

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Define doesnt work:
    Java Code:
    		x = input.InputInt();
    		System.out.println(x);
    		y = input.InputInt();
    		System.out.println(x);
    You are also printing x twice there.

  5. #5
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by staykovmarin View Post
    Define doesnt work:
    Java 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

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Post your current code (also a partial exception is really of no help).

    I have a feeling that InputInt is still static.

  7. #7
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    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? :o

  8. #8
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Post your Min class too?

    Use the code tags: [ code][/ code]

  9. #9
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    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);
    		
    	}
    }

  10. #10
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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();
    	}
    }
    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.

  11. #11
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by staykovmarin View Post
    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();
    	}
    }
    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

Similar Threads

  1. How to read input from console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-16-2008, 11:57 PM
  2. How to read input from the console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-06-2008, 08:41 PM
  3. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:52 PM
  4. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:50 PM
  5. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •