Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Unhappy Detect the ENTER key being pressed

    Hi all I've wriiten a program that asks for various user inputs.

    I want to this process to be repeated until the user presses the ENTER key in a certain field

    System.out.println("========== Please select a course to modify ==========");
    System.out.println("** If course code is not above enter new course code **");
    System.out.println("** Press ENTER to QUIT **");
    updateCourse = input.next();
    if(//user presses enter)){
    System.out.println("Finished");

    }else{
    for(int j = 0; j < testStudent.getAllCourses().size(); j++){
    if(updateCourse.equals(testStudent.getCourses(j))) {
    System.out.println("Please enter an updated mark for this course");
    newMark = input.nextInt();
    testStudent.updateMark(updateCourse, newMark);
    allStudents.add(testStudent);

    }
    }
    for(int y = 0; y < allStudents.size(); y++){
    System.out.print(allStudents.get(y).prepareFullPri nt());
    for(int k = 0; k < allStudents.get(y).getAllResults().size(); k++){
    System.out.print(":" + allStudents.get(y).getCourses(k) + "-" + allStudents.get(y).getMark(k));
    }
    System.out.println();
    }
    }


    Hope someone can help thanks.:confused:

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You want to implement KeyListener...
    Who Cares... As Long As It Works...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by markw8500 View Post
    You want to implement KeyListener...
    But that would require a GUI program. His is a simple console program.

    To the OP: I'm not sure that console will do what you want here. Perhaps you want to create a GUI, but I think not, not at your stage of programming. I think you'll want to use a String such as "quit" for the user to type in when he wants the loop to end.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, key listeners are not working for console application. Either you have to convert your application into UI or use a simple key-word implementation as Fubarable says.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    O.o it can't be this simple. But it is :p If the users presses enter, the length of the String is 0. See the following code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    public class ExitOnEnter {
    
    	public static void main(String[]args){
    		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    		while(true){
    			System.out.println("Press enter to exit");
    			String s = null;
    			try {
    				s = br.readLine();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			if(s.length() == 0){
    				System.out.println("Exiting...");
    				System.exit(0);
    			}
    		}
    	}
    }
    This just runs until the String returned has a length of 0, which means enter was pressed :)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    Thank you singing boyo; it just took some lateral thinking

    Legend.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually quite tricky that code. ;) Just read the NULL string from the console. If you have a space character this wont work. My suggestion is use of the exiting code, like type the command exit.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Hi ,
    As per Erangas suggestion ,I did small alteration on Singing Boyo code in the if part.Please go thru.


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;


    Java Code:
    public class ExitOnEnter {
    
    	public static void main(String[]args){
    		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    		while(true){
    			System.out.println("Press enter to exit");
    			String s = null;
    			try {
    				s = br.readLine();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			if((s == null)||
    				(s.length() == 0)
    					||(s.trim().equals(""))){
    				System.out.println("Exiting...");
    				System.exit(0);
    			}
    		}
    	}
    }
    Ramya:cool:

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems it's fine actually. But the checking of the length is not necessary, since you trim the string before validation for empty string.

    Java Code:
    			if(s == null){
    				System.out.println("Exiting...");
    				System.exit(0);
    			}

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    The console string won't ever be null though... it will be empty when enter is pressed, but not null. Therefore, checking only for null will not do anything(believe me, I tried it). If you also want to exit if there is a space, use if(s.trim().length() == 0) instead of if(s.length() == 0), but don't use null, it just won't work :p
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Another option to allow it to exit with unlimited spaces...

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    public class ExitOnEnter {
    	
    	public static void main(String[]args){
    		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    		while(true){
    			System.out.println("Press enter to exit");
    			String s = null;
    			try {
    				s = br.readLine();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			if(s.length() == 0 || isStringOfSpaces(s)){
    				System.out.println("Exiting...");
    				System.exit(0);
    			}
    		}
    	}
    	private static boolean isStringOfSpaces(String s){
    		for(char c : s.toCharArray()){
    			if(c != ' ')
    				return false;
    		}
    		return true;
    	}
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    > private static boolean isStringOfSpaces(String s){

    What's wrong with String#trim()?

    db

  13. #13
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Haha lol... trim() has never worked well for me, so I always end up making my own methods for it. Should have realized it would work here. trim() doesn't cut white spaces if they are only on one side, as far as I can tell. Maybe it was just some error in my other programs that I fixed later and didn't realize lol.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. get key pressed
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-26-2009, 09:10 PM
  2. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 09:30 PM
  3. can you help me with mouse pressed method please?
    By java_fun2007 in forum New To Java
    Replies: 4
    Last Post: 05-22-2008, 10:23 PM
  4. Detect loading of ImageIcon from URL?
    By barkster in forum Java Applets
    Replies: 1
    Last Post: 01-29-2008, 07:04 PM
  5. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 02:01 PM

Posting Permissions

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