Input without a new new


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Input without a new new

  1. #1
    Join Date
    Jan 2004
    Posts
    22

    Input without a new new

    I'm wondering how to get input without a line break.

    Here's what I made. It gets input all right, but there is a line break without any clear (to me) cause.

    Code:
    	int GetIntegerInput(String prompt, int min, int max) {
    		boolean valid;
    		String answer = null; 
    		int i=0;
    		do {
    			valid=true;
    			System.out.print(prompt+": ");
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    			try { 
    				answer = br.readLine(); 
    			} catch (IOException ioe) { 
    				System.out.println("IO error trying to read answer!"); 
    				System.exit(1); 
    			} 
    
    			try {
    				i = Integer.parseInt(answer);
    			} catch (java.lang.Exception ex) {
    				System.out.print("That is not a valid number. ");
    				valid=false;
    			} 
    			if (valid) {
    				if (i<min) System.out.print("That is too low. ");
    				else if (i>max) System.out.print("That is too high. ");
    			}
    		} while (!valid || i<min || i>max);
    		return Integer.parseInt(answer);
    	}
    Thanks in advance.

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    maybe because the user hits enter after each input? I don't know if there's a way to get input without the line break.

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: Input without a new new

    Originally posted by Oinkerwinkle
    I'm wondering how to get input without a line break.

    Here's what I made. It gets input all right, but there is a line break without any clear (to me) cause.

    Code:
    	int GetIntegerInput(String prompt, int min, int max) {
    		boolean valid;
    		String answer = null; 
    		int i=0;
    		do {
    			valid=true;
    			System.out.print(prompt+": ");
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    			try { 
    				answer = br.readLine(); 
    			} catch (IOException ioe) { 
    				System.out.println("IO error trying to read answer!"); 
    				System.exit(1); 
    			} 
    
    			try {
    				i = Integer.parseInt(answer);
    			} catch (java.lang.Exception ex) {
    				System.out.print("That is not a valid number. ");
    				valid=false;
    			} 
    			if (valid) {
    				if (i<min) System.out.print("That is too low. ");
    				else if (i>max) System.out.print("That is too high. ");
    			}
    		} while (!valid || i<min || i>max);
    		return Integer.parseInt(answer);
    	}
    Thanks in advance.
    i dont know what you mean.. the "int" primitive type is incapable of holding a line break character as well as an int..
    heres what happens when the user types something:

    every character pressed goes into a buffer (in the buffered reader)
    when return is pressed the buffered reader notices this as the end of line
    it is at this moment that the call to br.readLine() returns (because youve told it to read a line.. how else do you indicate the end of line, other than by pressing return?)
    after readLine() returns the method converts what was typed to an integer (if possible)

    nowhere does your java program receive or store a line break..

    if you are complaining that the console is advancing one line each time you press return; then yes.. it is.. that's Microsoft that did that when they wrote the DOS interpreter. you cannot alter that behaviour.

    if you require input without the return key being necessary, you must use read() instead of readLine() and deal with characters as they come in. in short its a pain in the *** and not worth the hassle
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Jan 2004
    Posts
    22
    if you are complaining that the console is advancing one line each time you press return; then yes.. it is.. that's Microsoft that did that when they wrote the DOS interpreter. you cannot alter that behaviour.
    Yup, that was it. Thanks for the clarification anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center