Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > While statement

Reply
Thread Tools

While statement

 
 
geletine
Guest
Posts: n/a
 
      01-23-2006
With the following piece of code, i would like to convert pounds to
dollars and then print to the screen, and then the program will ask me
if i want to convert more pounds to dollars, but what the program does
is asks for the pounds and prints the conversion and then ask if i
would like to convert more pounds, but unfortunately it does not ask me
again, i presume i am not understanding how the while loops works.
here is the example i am trying out.
the code was translated from pseudocode to java syntax.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class currencyc
{
public static void main (String args [])throws IOException
{
double pounds,dollars;
String money, answer,y;

answer = "y";


while (answer == "y")
{

money = JOptionPane.showInputDialog (" how many pounds do you
want to conver
t ? ");
pounds = Double.parseDouble(money);
dollars = pounds * 1.44;
System.out.println(dollars);
answer = JOptionPane.showInputDialog (" would you like another
go , yes/no?
");
}
}
}

 
Reply With Quote
 
 
 
 
Jeroen V.
Guest
Posts: n/a
 
      01-23-2006
You are comparing references to string instances where you should
compare the actual string instances

while(answer == "y") should be while(answer.equals("y"))


geletine wrote:
> With the following piece of code, i would like to convert pounds to
> dollars and then print to the screen, and then the program will ask me
> if i want to convert more pounds to dollars, but what the program does
> is asks for the pounds and prints the conversion and then ask if i
> would like to convert more pounds, but unfortunately it does not ask me
> again, i presume i am not understanding how the while loops works.
> here is the example i am trying out.
> the code was translated from pseudocode to java syntax.
>
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import java.io.*;
> public class currencyc
> {
> public static void main (String args [])throws IOException
> {
> double pounds,dollars;
> String money, answer,y;
>
> answer = "y";
>
>
> while (answer == "y")
> {
>
> money = JOptionPane.showInputDialog (" how many pounds do you
> want to conver
> t ? ");
> pounds = Double.parseDouble(money);
> dollars = pounds * 1.44;
> System.out.println(dollars);
> answer = JOptionPane.showInputDialog (" would you like another
> go , yes/no?
> ");
> }
> }
> }
>

 
Reply With Quote
 
 
 
 
zero
Guest
Posts: n/a
 
      01-23-2006
"geletine" <> wrote in news:1138041018.041233.9440
@z14g2000cwz.googlegroups.com:

>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import java.io.*;


Jeroen already gave you the answer to your problem, but I'd like to
offer some additional advice.

What classes of package java.awt are you using? Examine all your import
statements, and see if you really need them. Don't import packages you
don't use. And when you only need one or two classes from a package,
import only those classes, not the whole package.

> public class currencyc


For readability, you should always follow the convention of starting
class names with a capital letter.

> public static void main (String args [])throws IOException


Where is that IOException being thrown? Which statement in your main
method throws it? You shouldn't have a method throw an exception unless
it actually has a throw statement, or if a method it uses throws one.
Also, main should never throw an exception. Catch any exceptions that
are thrown inside your main method, and handle them gracefully.

> double pounds,dollars;
> String money, answer,y;


Avoid using package access when private will suffice. In particular,
use "private double pounds". Even better, when you only need a variable
in one method, declare it as local in that method.
Where do you use the y variable? Don't declare variables you don't
need.

> pounds = Double.parseDouble(money);


One of the 10 commandments of C/C++ is "always check your input". I
would say this goes for every programming language. If the user enters
"hello" into the input dialog, you should be able to recover from this.
If your course hasn't covered exceptions yet, ignore this comment.

Good luck with your studies, and have fun
 
Reply With Quote
 
geletine
Guest
Posts: n/a
 
      01-23-2006
Thank you for your explanation, i thought i need to use awt libaries
too, where as swing will suffice?
public static void main (String args [])throws IOException
i used that to catch any input output errors, hence i imported the
import output libary.
when i say catch, ive heard of the catch and try methods, but i presume
that line covers a whole libary of errors so the need for me to specify
what to catch in such a small program is not needed.
i have not learnt private access at the moment, i may guess that
private means that no other classes can acces this particular class, in
this program public is not a problem for me.
the y variable is for the user if they want to repeat the conversion,
otherwise what would signify a repeat of the loop?
lastly i have not included error checking as you lastly stated, at the
moment i am only learning loops.





zero wrote:

> "geletine" <> wrote in news:1138041018.041233.9440
> @z14g2000cwz.googlegroups.com:
>
> >
> > import java.awt.*;
> > import java.awt.event.*;
> > import javax.swing.*;
> > import java.io.*;

>
> Jeroen already gave you the answer to your problem, but I'd like to
> offer some additional advice.
>
> What classes of package java.awt are you using? Examine all your import
> statements, and see if you really need them. Don't import packages you
> don't use. And when you only need one or two classes from a package,
> import only those classes, not the whole package.
>
> > public class currencyc

>
> For readability, you should always follow the convention of starting
> class names with a capital letter.
>
> > public static void main (String args [])throws IOException

>
> Where is that IOException being thrown? Which statement in your main
> method throws it? You shouldn't have a method throw an exception unless
> it actually has a throw statement, or if a method it uses throws one.
> Also, main should never throw an exception. Catch any exceptions that
> are thrown inside your main method, and handle them gracefully.
>
> > double pounds,dollars;
> > String money, answer,y;

>
> Avoid using package access when private will suffice. In particular,
> use "private double pounds". Even better, when you only need a variable
> in one method, declare it as local in that method.
> Where do you use the y variable? Don't declare variables you don't
> need.
>
> > pounds = Double.parseDouble(money);

>
> One of the 10 commandments of C/C++ is "always check your input". I
> would say this goes for every programming language. If the user enters
> "hello" into the input dialog, you should be able to recover from this.
> If your course hasn't covered exceptions yet, ignore this comment.
>
> Good luck with your studies, and have fun


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-23-2006

"zero" <> wrote in message
news:Xns9754CABFC3434zerothishi@195.130.132.70...
>
> Also, main should never throw an exception. Catch any exceptions that
> are thrown inside your main method, and handle them gracefully.


I disagree with this. If the main method encounters an exception that it
cannot handle in any reasonable way, it should throw that exception.

- Oliver


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-23-2006

"geletine" <> wrote in message
news: oups.com...
>
> the y variable is for the user if they want to repeat the conversion,
> otherwise what would signify a repeat of the loop?


In the code you posted earlier in this thread, the variable called "y"
is never used. You stored the user's answer in a variabe called "answer".

- Oliver


 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      01-23-2006
"geletine" <> wrote in
news: oups.com:

> Thank you for your explanation, i thought i need to use awt libaries
> too, where as swing will suffice?
> public static void main (String args [])throws IOException
> i used that to catch any input output errors, hence i imported the
> import output libary.
> when i say catch, ive heard of the catch and try methods, but i
> presume that line covers a whole libary of errors so the need for me
> to specify what to catch in such a small program is not needed.
> i have not learnt private access at the moment, i may guess that
> private means that no other classes can acces this particular class,
> in this program public is not a problem for me.
> the y variable is for the user if they want to repeat the conversion,
> otherwise what would signify a repeat of the loop?
> lastly i have not included error checking as you lastly stated, at the
> moment i am only learning loops.
>


You only need to import the libraries you actually use. In this
program, you don't use any awt classes, and only JOptionPane from swing.
So, all you need is:

import javax.swing.JOptionPane;

As for the IOException, you don't need it. You'll only need it when you
use actual stream I/O, like files or sockets. Until you learn about
exceptions, just stay away from the altogether. They will only confuse
you and make learning the rest harder.

Since you haven't learned about access modifiers I assume your course
starts with structural language elements (if/else, while, for, methods),
and OO principles will come later. I feel quite strongly about this,
but as student you probably can't do much about it. IMO teaching
structural programming first is completely and utterly wrong. It may be
historically correct, but not from an educational standpoint.

Anyway that would be a discussion I'd need to have with your teacher,
not you. private indeed means that other classes can't use that
variable. It is a fundamental concept of OOP to keep the privileges as
tight as possible. But if you haven't learned this yet, just ignore it.

Lastly, you seem to be confusing the variable y with the String "y".
You are not using variable y declared in

String y;

You are however using the string "y", in the loop condition. But this
is not the variable y.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      01-23-2006
"Oliver Wong" <> wrote in
news:e2aBf.124818$6K2.33604@edtnps90:

>
> "zero" <> wrote in message
> news:Xns9754CABFC3434zerothishi@195.130.132.70...
>>
>> Also, main should never throw an exception. Catch any exceptions
>> that are thrown inside your main method, and handle them gracefully.

>
> I disagree with this. If the main method encounters an exception
> that it
> cannot handle in any reasonable way, it should throw that exception.
>
> - Oliver
>
>
>


In programming there are no absolutes of course. If I say "never", I
actually mean "never unless you are an experienced programmer and have a
compelling reason".

In general though, I think there is little use in main throwing an
exception. It would only be useful if the main method is called
programmatically, which is not typically the case.
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-23-2006

"zero" <> wrote in message
news:Xns9754D14DDD547zerothishi@195.130.132.70...
> "Oliver Wong" <> wrote in
> news:e2aBf.124818$6K2.33604@edtnps90:
>
>>
>> "zero" <> wrote in message
>> news:Xns9754CABFC3434zerothishi@195.130.132.70...
>>>
>>> Also, main should never throw an exception. Catch any exceptions
>>> that are thrown inside your main method, and handle them gracefully.

>>
>> I disagree with this. If the main method encounters an exception
>> that it
>> cannot handle in any reasonable way, it should throw that exception.

>
> In programming there are no absolutes of course. If I say "never", I
> actually mean "never unless you are an experienced programmer and have a
> compelling reason".
>
> In general though, I think there is little use in main throwing an
> exception. It would only be useful if the main method is called
> programmatically, which is not typically the case.


Maybe I am biased, because I was specifically in the situation of
wanting to call the main method of some 3rd party code, and wanted to handle
the exception situation it encountered. The 3rd party code would catch the
exception and then call system.exit(-1), thus killing my code as well as its
own.

I also don't like code which catches exceptions, and silently ignores
them. Or that simply prints that some exception has occured without
providing a stack trace.

My debugger will automatically pause program execution when it detects
an uncaught exception; but if something catches the exception, my debugger
assumes that the exception was correctly handled and so doesn't pause
program execution. So when you're debugging someone else's code, and you
just see "Exception occured" on standard error, you have to step through a
hell of a lot of code trying to locate what exception is getting thrown and
where.

- Oliver


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-23-2006
On 23 Jan 2006 10:30:18 -0800, "geletine" <>
wrote, quoted or indirectly quoted someone who said :

> answer = JOptionPane.showInputDialog (" would you like another
>go , yes/no?
>");

try inserting System.out.println( "{" + answer + "}" );

just after that line.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57