Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How can you do this?  (Read 1744 times)
0 Members and 1 Guest are viewing this topic.
Offline i30817

Junior Member





« Posted 2011-05-15 21:13:59 »

the new http://download.oracle.com/javase/6/docs/api/java/io/Console.html class seems ok for user input from the console, however, it seems to provide no way to read a line but with the default already filed in (so the user only has to press enter to confirm or delete to modify).

Is it possible?

I guess it should with the Robot class and a thread, and translating the strings to keycodes (which would be a problem since i don't control them), but is there another better way?
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-16 03:40:29 »

Are you saying you want the user input to be already filled in so all the user has to do is press enter and that String is sent to the program? That is no possible with any class in Java. With your Robot suggestion, you could probably use the "Robot.keyPress(int)" and "Robot.keyRelease(int)" methods to type the default characters. For the parameter, you would use the constants in the class java.awt.event.KeyEvent.

Offline cylab

JGO Ninja


Medals: 38



« Reply #2 - Posted 2011-05-16 15:33:33 »

The standard way of doing it, is to write the default into the label (like in brackets or something) and just check for an empty line in the code that handles the input.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-05-17 01:25:10 »

Yeah looking back at this, using Robot is kind of non-efficient and silly.
cylab pointed out the best way by checking for an empty input:
1  
2  
3  
String s = console.readLine();
if(s.equals(""))
   s = "default value";

Offline i30817

Junior Member





« Reply #4 - Posted 2011-05-18 06:48:21 »

Yeah looking back at this, using Robot is kind of non-efficient and silly.
cylab pointed out the best way by checking for an empty input:
1  
2  
3  
String s = console.readLine();
if(s.equals(""))
   s = "default value";


Which is what i ended up using, but not what i want. I wanted the user to see the text filled in not, to have to make the mental leap (empty == filled with default). I even tried to replace System.in with a custom inputstream that had both A string at first and the normal System.in chained, but it never ended up displaying the string. It may flush it before it does a readline? Bizarre nevertheless.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-05-18 07:14:28 »

This is not possible in any method I can think of. Using Robot is the only probable solution.
However, don't try to delve deep into Console IO because it won't do you much. GUI applications are the norm and you need to just learn the basics and move on to Swing.

Offline Sinuath

Junior Member


Medals: 2



« Reply #6 - Posted 2011-05-18 07:30:49 »

Umm i'm not a very good programmer and a bit new to java but i think i know what your asking..

it's a bit jury rigged but why don't you make a loop clearing the screen and displaying the question with the user input variable behind it until the user hits enter? if you throw a key listener on there as well you could append and delete the default string as you wanted.

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-05-18 13:34:19 »

@Sinuath
This is Console IO not GUI.

Offline Sinuath

Junior Member


Medals: 2



« Reply #8 - Posted 2011-05-18 14:40:39 »

I guess i just assumed that this Console IO thing was basically that command prompt window thing.   

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-05-18 19:22:13 »

Console IO is the command prompt. It is the standard output, standard input, and standard error streams. What you described is GUI application with text fields, when you said "clearing the screen" and "key listener" while you can't do either with Console IO.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sinuath

Junior Member


Medals: 2



« Reply #10 - Posted 2011-05-18 21:08:27 »

nah i wasn't talking about a text box. I just assumed if you could do it in C you could do it in java. used to clear screen with a system call.
 
and the whole concept seemed simple enough to be possible in java.

Hey [you][/you], you should totally check out my boring Site ~ http://davediel.com/chris
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-05-19 02:09:15 »

Oh it's possible in C? I thought it wasn't possible at all.

After much Googling around, it seems as if you cannot set a predetermined value for user input on the console. This is because Java doesn't assume that it is writing to the command line but is writing to the standard streams. If it did, then it would have to have specialized code for each platform.

Also, if you want to clear the command line, just output a bunch of empty lines.

Offline i30817

Junior Member





« Reply #12 - Posted 2011-05-19 20:28:35 »

It's kind of pathetic. Well whatever.

I do know swing fairly well. This is in the newbies section... just because.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-05-19 23:47:56 »

hehe Tongue

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (16 views)
2014-07-24 01:59:36

Riven (16 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (16 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!