Forums Register Login

Complete noob stuck on trying to create first Java programme using ScannerClasses

+Pie Number of slices to send: Send
I am just starting to learn programming with Java as my first language, using NetBeans as my IDE. I am trying to learn about "Input Readers and Variables"
I am trying to make a command line programme for revision (not homework or anything like that) however what I want is out of scope of what I have learned so far.

The Functional Requirements of the programme are: take user input of five letters, one at a time and print them back
My own added Non-functional requirements are that it must only take A-Z or a-z English characters and only one, and an error message is displayed when an incorrect input is placed.

What I have right now is using Strings but I suspect that may not be the right thing to use, not sure as I don't really understand much at all. I have not done the Print back bit yet, I know how to do that. The problem is what I have now will accept any kind of input.



What would be the simplest way to do what I want with this? I was thinking maybe use 'char' but I don't know how to use that or even if it can be used with scanner classes.
Indications of what I should read up on and/or other exercises I should try would be appreciated.
+Pie Number of slices to send: Send
Welcome to the Ranch, and thanks for using code tags.

You probably want an if statement or statements that checks whether the input is one character long and is a letter. Since you will be repeating this five time, you might want to put it in a method. Then use some kind of loop (I would use a do/while loop but there are some people who think this is a bad structure) to ask the question again until the input is correct.

And this is one time where try/resource is actually not the best thing to do, because System.in should never be closed. Your IDE will complain but that's all right.
+Pie Number of slices to send: Send
Welcome to the Ranch

There isn't a simplest way. You can get most of the number types from the keyboard directly with a Scanner with its nextXXX methods. You can read about them in the Scanner documentation. Unfortunately you won't find a nextChar method. I know. I have tried it myself. Possibly because Scanners work with “tokens” and you can't expect a “token” to consist of a single char.
The nearest to that is: you can get a String with next(). Then if you look through the String documentation and find which methods it has, there is one which will tell you the char at a particular location. Remember the first location is called 0, like in arrays.

+Pie Number of slices to send: Send
Thank you to all who have replied so far.

Knute Snortum: And this is one time where try/resource is actually not the best thing to do, because System.in should never be closed. Your IDE will complain but that's all right.


To be honest that "try" thing is only there because NetBeans wanted it there; this being only my fourth day in I have no idea what it is or what it does. My course material so far has not explained much of what it is getting me to type.

I will look into the documentation linked now.
+Pie Number of slices to send: Send
You should beware of hints from your IDE if you don't know what they mean. Another reason why we recommend beginners not to use IDEs.
+Pie Number of slices to send: Send
 

Avalon Stevens wrote:I am just starting to learn programming with Java as my first language, using NetBeans as my IDE


Obvious benefit in using IDE's is that building and running code can be easy, because of auto complete functions, auto imports and many other nice features.
The "negative" side of it, that you are not getting direct experience in repeatedly creating the complete structure of the commands (writing methods, importing classes, compiling programs), which makes your programming learning process slower.

I know that Java Ranch got something useful for you to know IDE's (<- read about it)
+Pie Number of slices to send: Send
If I understand you correctly you want to challenge yourself by adding some controls to your program. Here are some of my ideas.
- If you're following a class or studybook for this it is nice to challenge yourself, but be sure not to overpace yourself. It is easy to get ahead of yourself and miss some crucial fundamentals in the beginning.

- For solving this I think 2 things are quite helpfull thinks to master: Conditional statements and looping. These 2 things are commonly explained in Youtube tutorials on beginning Java. Googling it should give you enough hits.

gl
+Pie Number of slices to send: Send
Thanks for the replies so far. I accidentally reformatted my home partition so I am doing everything over again.
I was rushing through trying to cram stuff in as I am way behind in uni, being in week 7 now, I know that it won't work because I will never catch up, failing is inevitable. But that is OK, I will just redo the whole semester and just use the time I have to get more familiar with programming for when I repeat.

Anyway I think that I should clarify.
I am working though PDFs from my uni that I think are taken from a book "Introduction to Java" by S.Batsas & P. Snape, I don't know if any of you know it. Googling it showd other institutions using it but no books by that name by those authors anyway..
In the second chapter the first review exersise is this: "Create a Java application that prompts the user to enter five letters of the alphabet one at a time, and
then displays the group of letters to the screen. Name the file TestLetters.java"

The problem is with all that I know so far you could type in any damn thing and it will accept it, which is why I wanted to find out how to define parameters of input for ScannerClasses.

I am going back to the beginning and I will be reading those links along the way, will post back here if I get stuck.
+Pie Number of slices to send: Send
You can enter any keystrokes you like and the Scanner object will recognise them as keystrokes. Why should you try to determine whether a particular keystroke is used?
What Scanner does is recognise that the next token is a particular type, e.g. an int. It does not work with individual keystrokes but with “tokens”, separated by “delimiters”. There are some simple examples of how to use Scanners on the Scanner webpage; you can find it from the link I gave you two weeks ago.
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 641 times.
Similar Threads
Method creation trouble
One word scrabble.
Nested If statements, int and strings
Using Modulo in my program
Writing a compuer algorithm to solve a problem within 3 steps
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:35:43.