im a newbie to Java and in workshops what were doing is turning the below code to OO making it simipler. I was wondeirng if sumone if someone can turn the below code to OO soo i can get an idea of how code is turned into OO.
Individual methods aren't really "object-oriented" or not; object orientation is a property of entire programs, or at least of whole groups of classes. There are a number of problems with this code, but in no sense could you "make this OO" and improve it. At some level, all code, OO or not, has to execute a series of statements to get work done. I/O code like this, especially, more or less looks this way, and there isn't much you can do to change it.
While we're here, I could point out a few things really wrong with this code:
- "while (!false)" is needlessly complex; why not "while (true)" ? - short, meaningless variable names make code hard to understand - null message parameter to exception is asking for trouble; why not "", or no parameter? - Misuse of DataFlavorException type, which is related to drag-and-drop; use IllegalArgumentException - Terrible misuse of exception handling in general; throwing and catching an exception within a single method is silly. Just use "if/then!" - unnecessary loop label just adds to the confusion.
If I understand correctly what's happening here, you're just trying to prompt the user for a valid number which is less than or equal to "nop". I might write that like this (if I needed to write code that worked in JDK 1.4, as yours would; in JDK 1.5, you could use the "Scanner" class to simplify a little.)
Joined: Sep 27, 2006
Hi...this method is a flase basically less then 0 or numbers above 5 is a bad number.
heres what ive done its complaining about the the int h =-1 saying its duplicate. not sure what i have to do there and for some reason its complaining about the last syntax } why?
You should probably show the whole method to help us helps you.
Also please work on your formatting, your code is very hard to read without any indentations.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Sep 27, 2006
thats the full code i was going to attempt the true statement after i finsihed the false one.. but i kept on having the error on it. but ive listed the full code. in the above method which im doing it does not like int h = -1; also it complains about the last syntax for some reason? hope this helps [ October 01, 2006: Message edited by: steve low ]
author and iconoclast
You can only declare a variable once; "int h = -1" could simply be "h = -1", since h already exists.
But now we are getting into another style issue: methods should never be this long, and you should never reuse variables for many different purposes this way. This should be broken up into multiple small er methods.
Given this issue -- the multiple-declarations problem -- it's clear that this belongs in "Java in General (Beginner)," and not here.