I am stuck in this mess.

Hi guys,

First, I've done with checking whether the input is entered valid or not and for each number will do some operations but separately.

The problem is when I combine the two tasks, there is some things go wrong. For example, after enter the 1st number, it must allows me to enter the second one, but it go directly to the operation. I tried to figure out why it hapened like that, but seems I couldn't.

My codes are here:

import javax.swing.*;

publicclass AssessmentSeven

{

publicstaticvoid main (String[] args)

{

while (true)

{

String input_1=JOptionPane.showInputDialog("How many numbers do you want to convert?");

try

{

int numbers=Integer.parseInt(input_1);

if (numbers>0)

{

int [] dist =newint [numbers];

for (int i=0; i < numbers; i++)

{

String input_2=JOptionPane.showInputDialog("Enter distance "+(i+1));

dist[i]=Integer.parseInt(input_2);

while(true)

{

for (i=0; i < numbers; i++)

{

boolean done =false;

while(!done)

{

String input_3=JOptionPane.showInputDialog("Metres to convert: "+dist[i]+"\n1.Convert to kilometres \n2.Convert to inches \n3.Convert to feet \n4.Quit the conversion \n \nEnter your choice");

int choice=Integer.parseInt(input_3);

switch (choice)

{

case 1:double display_1=dist[i]*0.001;JOptionPane.showMessageDialog(null,dist+" metres = "+display_1+" kilometres");break;

case 2:double display_2=dist[i]*39.37;JOptionPane.showMessageDialog(null,dist+" metres = "+display_2+" inches");break;

case 3:double display_3=dist[i]*3.281;JOptionPane.showMessageDialog(null,dist+" metres = "+display_3+" feets");break;

case 4: done=true;break;

default: JOptionPane.showMessageDialog(null,"Invalid entry");break;

}

}

}

}

}

}

else

{

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

}

catch(NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

}

}

}

[4490 byte] By [Emotionsa] at [2007-11-27 6:51:42]
# 1
Suggestion: break that into logical chunks, into separate methods.I honestly can't even look at code that nasty.
Hippolytea at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 2
Merge those lines and we have a pretty good candidate for a java obfuscation contest :).
kevjavaa at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 3
IM STUCK IN A GLASS CAGE OF EMOTIONS
TuringPesta at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 4
JUST THROW YOUR HEART OF STONE AND BE FREE
kevjavaa at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 5

Oh guys, I know that my codes are so messy, but that's all I can do when I am a Java beginner.

Ok, in simple English, the program is like that:

1. Prompt the user to enter a valid number (if not, there a message will be displayed, and redisplay the prompt to ask the user enter a number again until it is valid number).

2. When the number was valid, prompt the user to enter another number (also must check whether it is valid or not, this part I missed).

3. If the number is valid, do some operations.

Emotionsa at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 6
THWEEEEEEEEEEEEEEET!!! NOW THEN, NOW THEN ... WHAT'S ALL THIS THEN?! WE'LL HAVE NOT MORE SILLY POSTS! THOSE PREVIOUS POSTS ARE ALL JUST SILLY!
abillconsla at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 7

Break it up into smaller tasks and write methods that perform those tasks.

> Oh guys, I know that my codes are so messy, but

> that's all I can do when I am a Java beginner.

>

> Ok, in simple English, the program is like that:

> 1. Prompt the user to enter a valid number (if not,

> there a message will be displayed, and redisplay the

> prompt to ask the user enter a number again until it

> is valid number).

Write a method that prompts for input, parses the input, and either returns it if it's valid, or returns an error if not (either a sentinel value like -1, or throws an exception.

> 2. When the number was valid, prompt the user to

> enter another number (also must check whether it is

> valid or not, this part I missed).

Check the return value from that first method. If it's valid, call the method again to get the second number.

> 3. If the number is valid, do some operations.

Are you picking up on the pattern here?

hunter9000a at 2007-7-12 18:26:10 > top of Java-index,Java Essentials,Java Programming...
# 8

> Oh guys, I know that my codes are so messy, but

> that's all I can do when I am a Java beginner.

>

> Ok, in simple English, the program is like that:

> 1. Prompt the user to enter a valid number (if not,

> there a message will be displayed, and redisplay the

> prompt to ask the user enter a number again until it

> is valid number).

> 2. When the number was valid, prompt the user to

> enter another number (also must check whether it is

> valid or not, this part I missed).

> 3. If the number is valid, do some operations.

I like English. Let's start with this:

public class Homework {

public void doSomeOperations() {

// Do stuff.

}

public int getNumber() {

// Prompt, return a number.

}

public boolean isValid(int num) {

// Return true if the number is valid, false if not.

}

public void doStuff(int num) {

int num = 0;

do {

num = getNumber();

} while ( !isValid(num) );

doSomeOperations(num);

}

public static void main(String [] args) {

Homework homework = new Homework();

homework.doStuff();

}

}

My general rule is that any code with more than four indentation levels is smelly, and probably should be revisited :).

kevjavaa at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 9

We can start by having some respect for the sanctity of line breaks.

import javax.swing.*;

public class AssessmentSeven{

public static void main(String[] args){

new AssessmentSeven();

}

public AssessmentSeven(){

while(true){

String input_1=JOptionPane.showInputDialog("How many numbers do you want to convert?");

try{

int numbers=Integer.parseInt(input_1);

if(numbers>0){

int [] dist = new int [numbers];

for (int i=0; i < numbers; i++){

String input_2=JOptionPane.showInputDialog("Enter distance "+(i+1));

dist[i]=Integer.parseInt(input_2);

while(true){

for (i=0; i < numbers; i++){

boolean done = false;

while(!done){

String input_3=JOptionPane.showInputDialog(

"Metres to convert: " + dist[i] +

"\n1.Convert to kilometres \n2.Convert to inches \n3.Convert to feet " +

"\n4.Quit the conversion \n \nEnter your choice");

int choice=Integer.parseInt(input_3);

switch (choice){

case 1:

double display_1=dist[i]*0.001;

JOptionPane.showMessageDialog(null,dist+" metres = "+display_1+" kilometres");

break;

case 2:

double display_2=dist[i]*39.37;

JOptionPane.showMessageDialog(null,dist+" metres = "+display_2+" inches");

break;

case 3:

double display_3=dist[i]*3.281;

JOptionPane.showMessageDialog(null,dist+" metres = "+display_3+" feets");

break;

case 4:

done= true;

break;

default:

JOptionPane.showMessageDialog(null,"Invalid entry");

break;

}

}

}

}

}

} else {

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

} catch(NumberFormatException e){

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

}

}

}

TuringPesta at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 10
Now use some methods.
TuringPesta at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 11

> Oh guys, I know that my codes are so messy, but

> that's all I can do when I am a Java beginner.

No, not true.

At the very least, when you have this :if (..) {

a

b

for (...) {

d

e

while (...) {

g

h

}

}

}

you can turn it into something like

if (...) {

x(...);

}

void x(...) {

a

b

for (...) {

y()

}

}

etc. ...

You can always break out blocks of code--bodies of if, for, while, etc.--into their own methods. That's not a cure-all, and not always the right solution, but usually it's a good step toward cleaner code.

"for each item in this list, do all these steps." Well, what are all those steps supposed to accopmlish? Put them into a method with a descriptive name that says what they're supposed to accomplish.

> Ok, in simple English, the program is like that:

> 1. Prompt the user to enter a valid number (if not,

> there a message will be displayed, and redisplay the

> prompt to ask the user enter a number again until it

> is valid number).

> 2. When the number was valid, prompt the user to

> enter another number (also must check whether it is

> valid or not, this part I missed).

> 3. If the number is valid, do some operations.

Okay, so you've given us your assignment. What specific questions do you have?

jverda at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 12

> Okay, so you've given us your assignment. What

> specific questions do you have?

I am not asking you guys to do my homework. As I said before, I did check for the valid number as well as for the operations. But the problem is when I combine the two tasks together, it went wrongly.

Specifically, when enter the numbers of times want to convert, for example is 2, then the prompt must allow to enter the two distances instead ofjust one is allowed and go directly to the opearations.

Aslo, when I combined them together, some functions are went wrong. For example, case 4 is to exit the conversion for the first number, but it is non-functionality.

Emotionsa at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 13
> Aslo, when I combined them together, some functions> are went wrong. For example, case 4 is to exit the> conversion for the first number, but it is> non-functionality.Exactly what went wrong? What does "It is non-functionality" mean?
jverda at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 14
case 4 is to exit the operation for the first number and in order to operate for the second number, but it is done nothing. I couldn't even exit the program.
Emotionsa at 2007-7-12 18:26:11 > top of Java-index,Java Essentials,Java Programming...
# 15
Dont use "magic numbers".static final int EXIT = 4;case EXIT:
TuringPesta at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 16

> case 4 is to exit the operation for the first number

> and in order to operate for the second number, but it

> is done nothing. I couldn't even exit the program.

What do you mean it's doing nothing? Either some code is executing or the program exits. What code is executing?

Speaking of code, have you cleaned it up yet? I'm not even going to look at what you posted originally.

jverda at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 17
I'm going to seperate into some particular parts. And to be honest, this is my homework in the next few weeks include something that I haven't studied yet. I just want to be challenged. That's why i am stucked, that's why I am asking you guys to help me understand more about Java.
Emotionsa at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 18
We're happy to help, as long as you follow our suggestions, make it easy for us to help you, and show effort on your part.
jverda at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 19

Fill in the conversion code.

Why doesnt the (meters != Double.NAN) code work?

import javax.swing.*;

public class AssessmentSeven{

public static void main(String[] args){

new AssessmentSeven();

}

public AssessmentSeven(){

String input;

double meters;

int conversion;

input = JOptionPane.showInputDialog(

"Number: Meters to Convert" +

"\nQ: Quit");

if(!doExit(input)){

if((meters = getNumber(input)) != Double.NaN){

if((conversion = getConversionType(meters)) != EXIT){

doConversion(meters, conversion);

}

}

}

}

public boolean doExit(String input){

return (input == null) || (input.trim().toLowerCase().startsWith("q"));

}

public double getNumber(String input){

try{

return Double.parseDouble(input);

} catch(Exception e){

return Double.NaN;

}

}

public int getConversionType(double meters){

String input = JOptionPane.showInputDialog(

"Meters to convert: " + meters +

"\n1.Convert to kilometres" +

"\n2.Convert to inches" +

"\n3.Convert to feet" +

"\n4.Quit" +

"\n\nEnter your choice");

try{

int i = Integer.parseInt(input);

return (i > 0 && i < 4) ? i : EXIT;

} catch(Exception e){

return EXIT;

}

}

public void doConversion(double meters, int conversion){

switch(conversion){

case CONVERT_KM:

break;

case CONVERT_IN:

break;

case CONVERT_FT:

break;

default:

break;

}

}

public static final int CONVERT_KM = 1;

public static final int CONVERT_IN = 2;

public static final int CONVERT_FT = 3;

public static final int EXIT = 4;

}

TuringPesta at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 20

> Why doesnt the (meters != Double.NAN) code work?

Use Double.isNan(value) instead.

As for your question: http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3

<quote>

NaN is unordered, so the numerical comparison

operators <, <=, >, and >= return false if either or both

operands are NaN (15.20.1). The equality operator ==

returns false if either operand is NaN, and the inequality

operator != returns true if either operand is NaN

(15.21.1). In particular, x!=x is true if and only if x is NaN,

and (x<y) == !(x>=y) will be false if x or y is NaN.

</quote>

Hippolytea at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 21
I really appreciate what you guys have done for me as well as your worthy suggestions but the requirement is have to use arrays and for loop.I am totally going crazy with many loops or arrays.
Emotionsa at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 22
You can still use methods with loops and arrays hero.
TuringPesta at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 23

Here try this:

import javax.swing.*;

public class AssessmentSeven

{

public static void main (String[] args) throws NumberFormatException

{

boolean done = false;

while (!done)

{

String input_1=JOptionPane.showInputDialog("How many numbers do you want to convert?");

int numbers=Integer.parseInt(input_1);

if (numbers > 0)

{

int[] dist = new int[numbers];

for (int i=0; i < numbers; i++)

{

String input_2=JOptionPane.showInputDialog("Enter distance "+(i+1));

dist[i]=Integer.parseInt(input_2);

String input_3=JOptionPane.showInputDialog("Metres to convert: "+dist[i]+"\n1.Convert to kilometres \n2.Convert to inches \n3.Convert to feet \n4.Quit the conversion \n \nEnter your choice");

int choice=Integer.parseInt(input_3);

switch (choice)

{

case 1: double display_1=dist[i]*0.001;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_1+" kilometres");

break;

case 2: double display_2=dist[i]*39.37;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_2+" inches");

break;

case 3: double display_3=dist[i]*3.281;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_3+" feets");

break;

case 4: done= true;

break;

default: JOptionPane.showMessageDialog(null,"Invalid entry");

break;

}

}

done = true;

}

else

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

}

}

abillconsla at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 24

> I really appreciate what you guys have done for me as

> well as your worthy suggestions but the requirement

> is have to use arrays and for loop.

>

Nobody has suggested not to use them.

> I am totally going crazy with many loops or arrays.

Because you refuse to put any of it into methods. If you put it into methods as we have suggested many times, you can worry about one piece at a time. Then, in the loop, you can just say "do this piece" (by its meaningful name), while worrying about how to solve the larger step that uses this piece as one of its substeps, without worrying about the details of that substep because you've already taken care of them.

jverda at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 25
If you look at the code I posted I eliminated three of the loops you had.
abillconsla at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 26

> Here try this:

This is exaclty what I have done with this program, you can look at this:

import javax.swing.*;

public class arrays{

public static void main (String [] args){

String input = JOptionPane.showInputDialog("How many times do u want to enter numbers?");

int times = Integer.parseInt(input);

int [] numbers = new int [times];

for (int i=0; i<times; i++){

String input_1=JOptionPane.showInputDialog("Enter number "+(i+1));

numbers [i] = Integer.parseInt(input_1);

}

for (int i=0; i><times; i++){

JOptionPane.showMessageDialog(null,"Numbers you have entered is: "+numbers[i]);

}

while (true){

for (int i=0; i><times;i++){

boolean done=false;

while (!done){

String input_2=JOptionPane.showInputDialog(

"What do you want to do with the number "+numbers[i]+

"\n1.Add 2"

"\n2.Subtract 2"

"\n3.Multiply by 2"

"\n4.Quit the calculation"

"\n5.Quit program");

int choice=Integer.parseInt(input_2);

switch (choice){

case 1: int display_1=numbers[i]+2;

JOptionPane.showMessageDialog(null,"The number after adding 2 is :"+display_1);break;

case 2: int display_2=numbers[i]-2;

JOptionPane.showMessageDialog(null,"The number after subtracting 2 is :"+display_2);break;

case 3: int display_3=numbers[i]*2;

JOptionPane.showMessageDialog(null,"The number after multiplying by 2 is :"+display_3);break;

case 4: done=true;break;

case 5: System.exit(0);break;

default: JOptionPane.showMessageDialog(null,"Invalid entry");break;

}

}

}

}

}

}

But when I tried to control the value of the input, it went wrong.

And here are codes to control the value of the input:

import javax.swing.*;

public class Try

{

public static void main (String[] args)

{

while (true)

{

String input=JOptionPane.showInputDialog("Enter an integer");

try

{

int num=Integer.parseInt(input);

if (num>0)

{

JOptionPane.showMessageDialog(null,num);

break;

}

else

{

JOptionPane.showMessageDialog(null,"Invalid number");

}

}

catch(NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Invalid number");

}

}

}

}

Emotionsa at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 27
What are you doing? Why did you take the code I posted and then add another class?
abillconsla at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 28

> Because you refuse to put any of it into methods. If

> you put it into methods as we have suggested many

> times, you can worry about one piece at a time. Then,

> in the loop, you can just say "do this piece" (by its

> meaningful name), while worrying about how to solve

> the larger step that uses this piece as one of its

> substeps, without worrying about the details of that

> substep because you've already taken care of them.

I did not refuse what you have suggested me, but the problem is I never try more than one method that's why i don't know how to call one method from another. Sorry for this, but I haven't studied it yet.

Emotionsa at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 29
Putting all that asside for the moment ... did you try the code I posted. Unless I misunderstand your purpose, it should oughta do what you want.
abillconsla at 2007-7-21 22:07:43 > top of Java-index,Java Essentials,Java Programming...
# 30

For gods sake man, highlight all your code and press delete.

Start from scratch and solve one problem at a time.

You obviously need help with methods so let us help you.

But just posting 8 for/if/while loops all tangled up together and

trying to untangling it will get old quick.

TuringPesta at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 31

> If you look at the code I posted I eliminated three

> of the loops you had.

I knew that, but if you eliminate it, then when you enter the number <1 or some character, the program will be bugged.

Also, when you finish calculting the first number, it won't allow you to continue calculate the next number instead it will be exited.

Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 32

> For gods sake man, highlight all your code and press

> delete.

> Start from scratch and solve one problem at a time.

> You obviously need help with methods so let us help

> you.

> But just posting 8 for/if/while loops all tangled up

> together and

> trying to untangling it will get old quick.

Yes, I really need your help to understand about methods. Be my teacher. Thx :)

Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 33

> > If you look at the code I posted I eliminated

> three

> > of the loops you had.

>

> I knew that, but if you eliminate it, then when you

> enter the number <1 or some character, the program

> will be bugged.

>

> Also, when you finish calculting the first number, it

> won't allow you to continue calculate the next number

> instead it will be exited.

You obviously did not try the code, becuase it does not fail as you stated.

abillconsla at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 34
If you want to learn about methods, post #11 is a fine demonstration of a very simple sketch of an implementation.However, you don't really need that in this particular case because you are able to simplify it dramatically.
abillconsla at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 35
> You obviously did not try the code, becuase it does> not fail as you stated.Swear to GOD, I tried it, when enter a character, it bug
Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 36
> If you want to learn about methods, post #11 is a> fine demonstration of a very simple sketch of an> implementation.How to find the post#11 man?
Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 37
Yeah, but why did you enter a character ... I thought it only numbers you wanted - why did not not say you wanted characters too? What are you doing with characters in this app?
abillconsla at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 38
> > If you want to learn about methods, post #11 is a> > fine demonstration of a very simple sketch of an> > implementation.> > How to find the post#11 man?Post #11 is the eleventh post and is on the previous page - from jverd.
abillconsla at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 39
> Yeah, but why did you enter a character ... I thought> it only numbers you wanted - why did not not say you> wanted characters too? What are you doing with> characters in this app?Yes, character is not allowed
Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 40
glad I'm not stuck in this ******* this time around.
petes1234a at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 41
geeze. trbby is a disallowed word?
petes1234a at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 42
tarbaby, tarbabymust be just you ;-)I would have said "tarpit", anyhowdy.
Hippolytea at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 43
> geeze. trbby is a disallowed word?What is it? I dont understand.
Emotionsa at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 44

Okay, now it works to strain out non numeric input. But now it really really is messy and at this point you can start breaking up your code into methods. But at least it works.

import javax.swing.*;

public class AssessmentSeven

{

public static void main (String[] args)

{

boolean done = false,

bad = false;

int numbers = 0,

choice= 0;

while (!done)

{

for(bad = false; !bad;)

{

try {

String input_1=JOptionPane.showInputDialog("How many numbers do you want to convert?");

numbers=Integer.parseInt(input_1);

bad = true;

}

catch(NumberFormatException nfe) {

bad = false;

}

}

if (numbers > 0)

{

int[] dist = new int[numbers];

for (int i=0; i < numbers; i++)

{

for(bad = false; !bad;)

{

try {

String input_2=JOptionPane.showInputDialog("Enter distance "+(i+1));

dist[i]=Integer.parseInt(input_2);

bad = true;

}

catch(NumberFormatException nfe) {

bad = false;

}

}

for(bad = false; !bad;)

{

try {

String input_3=JOptionPane.showInputDialog("Metres to convert: "+dist[i]+"\n1.Convert to kilometres \n2.Convert to inches \n3.Convert to feet \n4.Quit the conversion \n \nEnter your choice");

choice=Integer.parseInt(input_3);

bad = true;

}

catch(NumberFormatException nfe) {

bad = false;

}

}

switch (choice)

{

case 1: double display_1=dist[i]*0.001;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_1+" kilometres");

break;

case 2: double display_2=dist[i]*39.37;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_2+" inches");

break;

case 3: double display_3=dist[i]*3.281;

JOptionPane.showMessageDialog(null,dist[i]+" metres = "+display_3+" feets");

break;

case 4: done= true;

break;

default: JOptionPane.showMessageDialog(null,"Invalid entry");

break;

}

}

done = true;

}

else

JOptionPane.showMessageDialog(null,"Invalid number! Try again please!");

}

}

}

abillconsla at 2007-7-21 22:07:49 > top of Java-index,Java Essentials,Java Programming...
# 45
Oh yes, now I know the reason why someone does not even just take a look at my code. Really messy.Then, teach me how to break them into methods, please.
Emotionsa at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 46

Firstly >>> Try this code out and make sure it does what you want.

Secondly >>> Look for repetituous code that can be separated functionally from the basic outline. This is similar to the way a logical outline is drawn up while you take notes in school; ex:

I. Main point 1 (discussion about shapes).

A. Circles.

i. circles are round.

ii. circles have a diameter.

iii. circles have a radius.

B. Polygons.

i. polygons have straight sides

ii. polygons have width.

iii. polygons have hieght.

C. etc.

II Main point 2.

Thirdly >>> go and look at post #11 and read the tutorials and start implementing your methods.

HTH, have a nice wkend.

~Bill

abillconsla at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 47
You did not tell me how do I go to post#11. There are tons of posts here.
Emotionsa at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 48
> You did not tell me how do I go to post#11. There are> tons of posts here.Reply 11 on this post, by Jverd.
kevjavaa at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 49
Could you guys just show me some examples. If not it is very hard for me to understand.Thx a lot
Emotionsa at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 50
I think everyone has broken out of your glass cage of Emotion.
TuringPesta at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 51
trbaby is an african american folktale. One of the Br'er Rabbit tales of Uncle Remus.Message was edited by: petes1234
petes1234a at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 52
> why i don't know how to call one method from another.Yes, you do. You're calling at least showInputDialog and parseInt in your original code.
jverda at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 53
> You did not tell me how do I go to post#11. There are> tons of posts here.It shows the reply number at the top of each post.
jverda at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 54

> Could you guys just show me some examples.

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].

James Gosling's [url=http://www.bookpool.com/sm/0321349806]The Java Programming Language[/url]. Gosling is

the creator of Java. It doesn't get much more authoratative than this.

jverda at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 55
> > You did not tell me how do I go to post#11. There> are> > tons of posts here.> > It shows the reply number at the top of each post.Yes but how to find 11 ? ;-P
Aknibbsa at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 56

My solution is that you run the following program. Keep running it til you understand.

While (javaUnderstanding == understanding.POOR)

{

OpenTextBook();

StudyTextBook();

GoToSunTutorial();

StudySunTutorial();

}

if (javaUnderstanding == understanding.TEACHABLE)

{

ComeBackToForum();

}

petes1234a at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 57
> Yes but how to find 11 ? ;-PFor that matter, why not just make 10 louder?
jverda at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 58
> > Yes but how to find 11 ? ;-P> > For that matter, why not just make 10 louder?LOL!And not even a thank you from the OP. B4N
abillconsla at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 59

Here is an example of a method.

int i = 0;

if (somethingHappened) {

doSomething();

}

void doSomething() {

i = 100;

}

Basically, this means if something happened, then do the operation specified in the method. If you want to return, replace void and specified what to return. Hope you understand what I am trying to say. :) Go to the first page and find (Reply #11 of total number of post) to find post 11.

danielmaua at 2007-7-21 22:07:54 > top of Java-index,Java Essentials,Java Programming...
# 60

Haha, you guys please don't tease me anymore, because I thought post#11 is same as thread.

Anyway, I have spent few hours to research about Methods and I have understood.

Below is one of my example:

import javax.swing.*;

public class Undestood

{

public static void main (String [] args)

{

String input=JOptionPane.showInputDialog("Enter your number:");

int number=Integer.parseInt(input);

int square= Sprt(number);

JOptionPane.showMessageDialog(null,Sprt(number));

}

public static int Sprt(int x)

{

return x*x;

}

}

Thanks all you guys for your kindly help

Emotionsa at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 61
kev,> My general rule is that any code with more than four indentation levels is smelly, and probably should be revisited :).Good rule.
corlettka at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 62
Just rename Sprt (WTF is a Sprt?) to squareOf and you're starting to cook.
corlettka at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 63
> Just rename Sprt (WTF is a Sprt?) to squareOf and> you're starting to cook.Yup, but whether food is delicious or not depends on how hard I put on it right? :p
Emotionsa at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 64

that was funpackage forums;

import javax.swing.JOptionPane;

public class DistanceConverter {

private enum Units {

Kilometres (0.001),//km per m

Inches (0.393700787), //' per m

Feet (3.2808399);//" per m

private final double perMetre;

private static final String choicesString = buildChoicesString();

Units(double perMetre) {

this.perMetre = perMetre;

}

double convert(double metres) {

return metres * this.perMetre;

}

private static String buildChoicesString() {

StringBuffer sb = new StringBuffer();

int i = 1;

for (Units units : Units.values() ) {

sb.append(" "+(i++)+". "+units+"\n");

}

sb.append(" "+i+". Quit\n");

return sb.toString();

}

public static Units chooseUnits() throws InterruptedException {

do {

switch (getIntFromUser(Units.choicesString)) {

case 1: return(Kilometres);

case 2: return(Inches);

case 3: return(Feet);

case 4: throw new InterruptedException("Quit!");

default: JOptionPane.showMessageDialog(null,"Try again numbnutts!");

}

} while(true);

}

}

public static void main (String[] args) {

do {

try {

int n = 0;

n = getIntFromUser("How many distances do you want to convert?");

if(n<1) break;

// get an array of n distance

double[] distances = new double[n];

for (int i=0; i<n; i++) {

distances[i] = getDoubleFromUser("Enter distance "+(i+1));

}

Units units = Units.chooseUnits();

StringBuffer answers = new StringBuffer();

for (int i=0; i><n; i++) {

double d = distances[i];

answers.append(d+" metres = "+units.convert(d)+" "+units+"\n");

}

JOptionPane.showMessageDialog(null,answers);

} catch(InterruptedException e) {

break;

} catch(Exception e) {

e.printStackTrace();

}

} while(true);

}

private static int getIntFromUser(String prompt) {

do {

try {

String response=JOptionPane.showInputDialog(prompt);

return(Integer.parseInt(response));

} catch(NumberFormatException e) {

JOptionPane.showMessageDialog(null,"An integer value is required. eg: 10");

}

} while(true);

}

private static double getDoubleFromUser(String prompt) {

do {

try {

String response=JOptionPane.showInputDialog(prompt);

return(Double.parseDouble(response));

} catch(NumberFormatException e) {

JOptionPane.showMessageDialog(null,"A double value is required. eg: 3.142");

}

} while(true);

}

}

It must be beer o'clock ;-)>

corlettka at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 65
By the way, I want to ask you guys, for example, when a prompt execute, but I dont want to continue, then I press "Cancel" button, it must allow me to exit the prompt. Then how to cope with this?
Emotionsa at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 66
Read the [url= http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html]API[/url] doco on JOptionPane
corlettka at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 67

> Read the

> [url=http://java.sun.com/javase/6/docs/api/javax/swing

> /JOptionPane.html]API[/url] doco on JOptionPane

Thx corlettk, I've got it :).

Anyway, after you guys censuring me about poorly understaing Java, I do love Java more :-p. Now I am ready to learn Java carefully and deeply.

Again, thx all :-p

Emotionsa at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 68
You are welcome. Now you've gotten corlettk and I to write working code for you, jverd providing you with an extensive list for you to learn from, and a number of other helpful replies. How's about smearing the proffered stars about?
abillconsla at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...
# 69
You got alot of help here but never gave out any stars ... why is that?
abillconsla at 2007-7-21 22:07:59 > top of Java-index,Java Essentials,Java Programming...