• Post Reply Bookmark Topic Watch Topic
  • New Topic

When ever the key "p" is pressed i want to ++variable and store it  RSS feed

 
danny ari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help with a code that will add 1 to a variable and store it anytime key "p" is pressed on the keyboard
Keep in mind i am very new to this, and need some pointers, would be very helpful
Here's what i have....








 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope scanner class javadocs will help you. You can read here
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will be working with a command line, and most command lines don't transmit until <enter> has been hit.
It's not listening on the keyboard, but the output from the terminal/command line app.
 
danny ari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:I hope scanner class javadocs will help you. You can read here


Link is invalid
 
danny ari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That code will be working with a command line, and most command lines don't transmit until <enter> has been hit.
It's not listening on the keyboard, but the output from the terminal/command line app.


Yes, you are right, how would i go about listening to the keyboard?
Thanks for the replies guys
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry dont know what happened.. Here it is updated one
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code doesn't compile as you are comparing string with the scanner variable. Please read javadocs, it is having method which helps you read from the
console.
 
danny ari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks im reading up on it now.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use the == operator on Strings; even if you get that code to compile you will not get it to recognise "p".
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:This code doesn't compile as you are comparing string with the scanner variable. . . .
Why won't it compile? I suspect it will actually compile.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't. kin is Scanner type variable and "p" is String. Both are not comparable.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the compiler recognises that that will always return false and you have if (false), so it won't compile.
 
danny ari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys i need you to slightly spoon feed me in the right direction, i am totally new to this.
how can i do this? i read that link but i don't understand the language enough to know how to use that information.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now look through the String documentation for a method to get a Char Array out of a String.
Iterate that array and see how many ps it has in.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi danny,

1. First of all, you got me confused with the class naming, you named it "Main". Try to avoid such a confusion as you have method named "Main".
Class name should be something descriptive, what kind of program it is - "LetterCounter" could be better as an example.

2. Try to follow java convention, and place the [] right after the String. 3. Variable names "pa", "ma" probably is not the best choise also, as it is not describe what they are meant to store.
At this stage you should try to name variables in the way, that you'd look at them, and wouldn't be any ambiguities without looking at the further code lines, what these variables stores.

3. As Campbell Ritchie already suggested, check, how the strings has to be compared. So in your case, you should compare "word" (if you'd use variable what Ritchie suggested) with "p".

4. Think about the case what you are going to do, when input is neither "p" or "m". So you need another block.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote: . . .
2. Try to follow java convention, and place the [] right after the String. . . .
Surely you mean
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote right about variable names and class names.

I didn't say to compare word with p; I said to turn it into an array of chars and see how many ps it contains. You can also count ms similarly.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I didn't say to compare word with p;
You're right about that.
Now look through the String documentation for a method to get a Char Array out of a String.
Iterate that array and see how many ps it has in.
Please explain that. Why is it for? I might need to go for coffee, since not wake up properly yet

I thought OP wants to compare keyboard input word.equals(...)
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought OP wanted to count the number of letters p and m.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought OP wanted to count the number of letters p and m.
Understood. Beside that, I still believe OP didn't describe for himself in english, what exactly has to be done. So it confuses which approach to the problem to choose.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought OP wanted to count the number of letters p and m.


Not quite.
They appear to want it to react to key presses, and increment the counters as the keys are pressed.
For which a Scanner is useless.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there isn't any way to make a terminal react to key presses, is there? You said so yesterday.

Which is why the press any key instruction is hardly ever seen nowadays
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But there isn't any way to make a terminal react to key presses, is there? You said so yesterday.

Which is why the press any key instruction is hardly ever seen nowadays


Not on a terminal no.
But with Swing I expect there is.

Isn't there a KeyListener or something similar?
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Swing (and AWT) you can use a key listener, yes. I think it would be attached to a particular component and would only listen when that component has focus. But it will listen for the any key. Even (unlike the older DOS variant) non‑printing keys (I think).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing is designed to work with Key Bindings, which is an appropriate approach for reacting to a specific keypress.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!