File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes checking user input?how to go about it ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking user input?how to go about it ?" Watch "checking user input?how to go about it ?" New topic
Author

checking user input?how to go about it ?

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Everyone,

I am requesting user input with the JOptioPane and need to have the input checked.There are more than just one input for this method, i have tried using if and else statements but i don't know
how to get it to go back to the original question without saving the input that was given first? i hope that just made some sense...here is the snippet of what i am working with.



The only idea i had was to have createBox(); if the input was wrong, which would go back to the beginning, but i would rather go back to that same question for new input...

Thanks
Mike
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
I'm under the impression that a non numeric value will throw an exception which would kick you out of your method altogether. Should take that into consideration as well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Why are you using boxes[0][0]? Why are you hard-coding that particular box? You will only ever create a box in that particular position if you haev that in your "create" method.

Why don't you write a "checkBoxInput" method which you can call from that method. Or even a getInput method, again to be called from those methods. Those methods I have mentioned might well be private static methods.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

I have [0][0] because in my main it is set up like this



Yes an extra method to check was an idea but i really have no idea how to go about it.Do i use an InputStreamReader and buffer reader?or is there a better way?I don' really get how that works with the reader/buffer.
The reason everything is static is because up to this point in my course , that is the only thing we have learned so doing this assignment i am trying to stick to what I have read up to this point in the Correspondence course book. I can't use class variables either.I suppose there must be a reason why they want it done that way?


Thanks for your reply
Mike
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Sorry ,

Forget what i just wrote before lol, i have too many versions of the same thing and confusing myself to no end!! and copied the wrong things there.But just a question then,

Is it possible to use only static to have a few methods which also pass the variables on? As far as i understand i can use "(int[][] argArray)" to do it?
And what would be the purpose for using just static objects and methods??Sorry i am having trouble understanding the static versus non-static...


Mike
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
When i made my version i just made a seperate method to handle all the input. Basically this method would keep asking and asking until the user gave a valid answer and only once a valid answer was given would that method end and therefore return the value to be stored in the array. No incorrect values would ever be stored in the array.

Also each time the user was supposed to input a number in the program they were given a multiple choice between one number and another, and as these numbers would always change depending on how many choices there were to choose from, i had to pass the information that the method needed to know what would be classed as valid. So for my method i would pass a min value and a max value and the method would use these to determine if the input was valid based. I also passed it a String which would be the message the JOptionPane would display, that meant i could keep showing JOptionPanes with the message until they entered the correct info. So this method would run WHILE the user's info they entered would be considered FALSE.

I don't know if this was the best way but it meant every time i added more things to the program it meant i didn't have to add any more code checking the input.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
If you are using an option pane, you wouldn't use Readers as well.
The reason the methods for checking input can be static is that they do not use, query or modify any data from any object. You pass the input, it is verified and passed on to the non-static method which sets up the box array.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

Thanks Neil and Campbell Sorry for the silly questions ,
i have been reading tonight more about Static methods to try to get an understanding of what they are and what they do.
I will be reading more tonight when i get home from work!!
I think one problem with learning a programming language like java is that a noob like myself, is very excited to get right into the coding,
and not understanding first what is going on in the background,
or don't understand allot of the rules that need to be followed in order for things to work properly.At least this is true i think in my case,
i can't speak for everyone of course
Having said that i still really just want to go home and get my code working ,but i will read bit more before i have to ask more goofy questions ...

Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
A static member belongs to the class, not the object. It is accessible from all instances of that class. There is only one copy of each static member. A static method cannot gain access to the fields in an instance. But all non-static methods can call a static method.

You will end up with something vaguely like this:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
. . . and when you get to do inheritance, never try to override a static method. It can't be done. You can write a static method with the same name, but that's hiding, not overriding, and works differently, so you should avoid it.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Thanks again Campbell!!

That is a good point to begin for me.First though gonna drink my Espresso and read a while.

Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checking user input?how to go about it ?
 
Similar Threads
question about using boolean to check a condition of an object?
Problem with indexing of an array
HELP!!!! how does Character.isLetter work? (need experts)
indexOf() error
New Objects keeping old values