• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

checking user input?how to go about it ?

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . 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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic