For Loops

Ok so i wrote a program that asks a user to enter a # in degrees f and the prog converts it to celsius. I want to be able to use a for loop to has the user enter a # in degrees farenheit X amount of times and then it will display all results! How would i use a for look to do that? here is the code:

publicstaticvoid main (String[] args)

{

String degreeS = JOptionPane.showInputDialog("Please Enter Your Temperature:");

int degree;

degree = Integer.parseInt( degreeS );

int celsius;

celsius = 5 * (degree - 32)/9;

JOptionPane.showMessageDialog(null, degree +"degrees Fahrenheit = " + celsius +" degress Celsius");

System.exit(0);

}

}

[1029 byte] By [Cheekz185a] at [2007-11-26 18:58:36]
# 1

Now is a godd time to learn that you don't have to shove all your code in the main method. You can write as many methods as you need. For example in your main method you have a loop. Inside that loop you call a method that asks the user to input the temp. Then call another method that converts that temp to Celsius.

class TempConverter {

public static void main(String[] args) {

loop {

getUserInput();

convertToCelsius();

}

}

getUserInput() {

}

convertToCelsius() {

}

}

Note: lots of detail omitted.

floundera at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 2

Can somebody tell me what is wrong with this. I was ablt to get the program to run an convert the degrees wit the for loop but now i need to put it into an applet>...

import java.awt.Graphics;// program uses class Graphics

import javax.swing.JApplet;// program uses class JApplet

import javax.swing.JOptionPane; // program uses class JOptionPane

public class Converter extends JApplet

{

String output;

String degreesResult = null;

public static final double PI = 3.14;

public void init()

{

String output;

String degreesResult = null;

double celsius;

double temperature = 0;

for (int x=0; x < 3; x++)

{

celsius = 5*(temperature - 32)/9;

degreesResult+=output.format("%d degrees farenheit = %.2f celsius.\n", temperature, celsius);

temperature += 1;

}

}

public void paint( Graphics g )

{

super.paint( g ); // call superclass version of method paint

g.drawString( output , 500, 300 );

} // end method paint

} // end class

Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 3
Any reason you're not displaying text in a JTextArea or other standard component?
DrLaszloJamfa at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 4

> String output;

This is the one which is a member of your class. You never set this, it's always null.

> String output;

This is another variable named 'output' in your init() method. It has nothing to do with the former 'output' variable. Its value is also undefined.

Later, you try to 'use' that variable:

> degreesResult+=output.format("%d degrees farenheit = %.2f celsius.\n", temperature, celsius);

What you're doing with 'output' there I have no idea. I guess the compiler let you get away with that just because format is a static method of the String class, so it's as if you typed:

degreesResult += String.format("...");

In your paint method, you are trying to use the 'output' member variable which is null. You never set ANY 'output' variable in that whole app, so it's pretty pointless to try to display it.

warnerjaa at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 5

What am i doing wrong!! This is frustrating. Basically i am trying to get the program to automatically take the integer 0 and count up in a for loop to 212. Each integer represents the Fahrenheit temp and it converts it to Celsius. I ned to make this in an applet and i am having trouble still...Can some1 please show me where it is wrong?

import java.awt.Graphics;

import javax.swing.JApplet;

import javax.swing.JOptionPane;

public class Converter extends JApplet

{

String outputStr;

public void init()

{

double celsius;

double temperature = 0;

String degreeResult;

for (int x=0; x < 212; x++)

{

celsius = 5*(temperature - 32)/9;

degreeResult =

outputStr.format("%d degrees farenheit = %.2f celsius.\n", temperature, celsius);

temperature += 1;

}

outputStr = degreeResult;

}

public void paint( Graphics g )

{

super.paint( g );

g.drawString( outputStr , 500, 300 );

}

}

Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 6
Did you understand and address the problems pointed out in reply 4?What exact problems are you having now?
jverda at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 7
THe program compiles but whn i try to open it inthe browser it wont output any information.I did create the html file to look for the class file
Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 8
It looks like it should output the data but its not....
Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 9
I don't know applets, so I can't help you there. Google for some applet tutorials or examples. Get them working first. Then figure out what's different between your code and what works.
jverda at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 10
Anybody else have any ideas?
Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 11

So you did that already? You found some examples and worked through the differences between them and your code, carefully making one change at a time until you found your error?

Or rather than do the work and understand the issue, you figure it's better to just wait for somebody to hand you the solution?

jverda at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 12
> Anybody else have any ideas?Its already been asked once: "Why aren't you displaying the output in a standard component such as a JTextField or a JLabel?"
Navy_Codera at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 13

Im trying to compare it to this code:

import java.awt.Graphics;// program uses class Graphics

import javax.swing.JApplet;// program uses class JApplet

import javax.swing.JOptionPane; // program uses class JOptionPane

public class JamesRadiusApp extends JApplet

{

String outputStr;

String outputStr2;

String outputStr3;

public static final double PI = 3.14;

public void init()

{

double radius;

double area;

double circumference;

String radiusString;

radiusString =

JOptionPane.showInputDialog("Enter the radius: ");

radius = Double.parseDouble(radiusString);

area = PI * radius * radius;

circumference = 2 * PI * radius;

outputStr = "Radius: " + radius + "\n";

outputStr2 = "Area: " + area + " square units\n";

outputStr3 = "Circumference: " + circumference

+ " units";

}

public void paint( Graphics g )

{

super.paint( g ); // call superclass version of method paint

// draw rectangle starting from (15, 10) that is 270

// pixels wide and 20 pixels tall

// draw results as a String at (25, 25)

g.drawString( outputStr , 25, 25 );

g.drawString( outputStr2 , 25, 50 );

g.drawString( outputStr3 , 25, 75 );

} // end method paint

} // end class RadiusApp

And it seems very similar but it just wont output.... I am using the book SMALL Java by Dietel and i was reading the powerpoint slides and it still doesnt help me figure the problem out!

Cheekz185a at 2007-7-9 20:39:09 > top of Java-index,Java Essentials,Java Programming...
# 14
ts already been asked twice: "Why aren't you displaying the output in a standard component such as a JTextField or a JLabel?"
DrLaszloJamfa at 2007-7-9 20:39:10 > top of Java-index,Java Essentials,Java Programming...