Results 1 to 13 of 13
- 05-15-2009, 02:25 AM #1
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:
- 05-15-2009, 03:30 AM #2
You want to implement KeyListener...
Who Cares... As Long As It Works...
-
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.
- 05-15-2009, 08:11 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
- 05-15-2009, 10:15 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
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); } } } }
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!
- 05-15-2009, 11:24 AM #6
Thank you singing boyo; it just took some lateral thinking
Legend.
- 05-15-2009, 12:19 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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.
- 05-15-2009, 12:38 PM #8
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:
- 05-15-2009, 06:16 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
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); }
- 05-16-2009, 03:04 PM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
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!
- 05-16-2009, 03:11 PM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
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!
- 05-16-2009, 07:41 PM #12
> private static boolean isStringOfSpaces(String s){
What's wrong with String#trim()?
db
- 05-16-2009, 08:48 PM #13
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
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
-
get key pressed
By prashant in forum NetworkingReplies: 1Last Post: 03-26-2009, 09:10 PM -
Waiting for a button to be pressed
By SomeGuyOverThere in forum New To JavaReplies: 6Last Post: 08-21-2008, 09:30 PM -
can you help me with mouse pressed method please?
By java_fun2007 in forum New To JavaReplies: 4Last Post: 05-22-2008, 10:23 PM -
Detect loading of ImageIcon from URL?
By barkster in forum Java AppletsReplies: 1Last Post: 01-29-2008, 07:04 PM -
key pressed event
By kavithas in forum New To JavaReplies: 7Last Post: 12-10-2007, 02:01 PM
Bookmarks