### Thread: Loop Issue

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
44
Rep Power
6

#### Loop Issue

I am trying to write a loop that will enable someone to convert a temperature of their choice to Celsius or Fahrenheit.

Code:
```package four3;
import java.util.Scanner;

public class Four3
{

public static void main(String[] args)
{
System.out.println("This program will perform a temperature conversion"
+ " for you.");
double degreesC, degreesF, temp;

Scanner keyboard = new Scanner(System.in);

do
{
System.out.println();
System.out.println("Enter a temperature to be converted:");
temp = keyboard.nextDouble();
System.out.println("Convert this temperature to C or F?");
tempType = keyboard.next();

if (tempType.equalsIgnoreCase("c"));
{
degreesC = (temp - 32) * 5 / 9;
System.out.println(degreesC);
System.out.println("Would you like another conversion?");
}

if (tempType.equalsIgnoreCase("f"));
{
degreesF = temp * (9 / 5) + 32;
System.out.println(degreesF);
System.out.println("Would you like another conversion?");
}

}
}```
Output:

run:
This program will perform a temperature conversion for you.

Enter a temperature to be converted:
50
Convert this temperature to C or F?
c
10.0
Would you like another conversion?
yes
82.0
Would you like another conversion?
yes

Enter a temperature to be converted:
90
Convert this temperature to C or F?
f
32.22222222222222
Would you like another conversion?
no
122.0
Would you like another conversion?
q
For some reason, the loop always does the Celsius conversion. Why isn't my equalsIgnoreCase working? When I run the program with just the Fahrenheit or just the Celsius, it functions exactly as I desire it too. After my first conversion, it seems to be converting the Unicode values of "yes", is it not? It seems be converting them to Fahrenheit. I know I'm very close, but I can't figure out why it is doing this.

I know there are other methods I could use to test for string values, but I'd like to know why equalsIgnoreCase is failing me.
2. An suggestion to make the output easier to read: add a String as a label in the println to describe what the number being printed is:
Code:
`System.out.println("degreesC="+degreesC);`
That String will tell you which block of code is being executed.

You need to compile the code with the javac program's -Xlint option to get some important warnings about the code.

D:\Java\jdk1.7.0.7\bin\javac.exe -Xlint TestCode12.java
Last edited by NormR; February 27th, 2013 at 03:51 PM.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
44
Rep Power
6
Code:
```package four3;
import java.util.Scanner;

public class Four3
{

public static void main(String[] args)
{
System.out.println("This program will perform a temperature conversion"
+ " for you.");
double degreesC, degreesF, temp;
int stringhelper, helper2;
Scanner keyboard = new Scanner(System.in);

do
{
System.out.println();
System.out.println("Enter a temperature to be converted:");
temp = keyboard.nextDouble();
System.out.println("Convert this temperature to C or F?");
tempType = keyboard.next();
stringhelper = tempType.charAt(0);

if ((stringhelper == 99) || (stringhelper == 67))
{
degreesC = (temp - 32) * 5 / 9;
System.out.println(degreesC + " Degrees Celsius.");
System.out.println("Would you like another conversion?");
if ((helper2==113) || ((helper2==81)))
{
break;
}
}

if ((stringhelper ==102) || (stringhelper==70))
degreesF = 0;
degreesF = temp * (9 / 5) + 32;
System.out.println(degreesF + " Degrees Fahrenheit.");
System.out.println("Would you like another conversion?");

}while((helper2 !=113) && (helper2 !=81));
}
}```
This code solves the problem, but it is pretty terrible. I bailed on the equalsIgnoreCase and opted for not statements which I have been told are bad ideas to use. Still have no idea why my initial if statements were not functioning as I wished them to.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
44
Rep Power
6
output (correct)

run:
This program will perform a temperature conversion for you.

Enter a temperature to be converted:
87
Convert this temperature to C or F?
f
119.0 Degrees Fahrenheit.
Would you like another conversion?
h

Enter a temperature to be converted:
65
Convert this temperature to C or F?
c
18.333333333333332 Degrees Celsius.
Would you like another conversion?
q
BUILD SUCCESSFUL (total time: 10 seconds)
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Here is an Elegant GUI Version

Code:
```import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class A extends JFrame implements ActionListener{
JTextField a;JTextField b;JButton c	;int i;int k;
public A(JTextField a,JTextField b,JButton c, int k){
i++;System.out.println(i);
//JFrame j=new JFrame();
if(k==0){this.k=0;
JLabel cel=new JLabel("CELSIUS");JLabel fah=new JLabel("FAHRENHEIT");
this.a=a;this.b=b;
this.c=c;c.setText("convert meah");b.setEditable(false);
setLayout(new FlowLayout());setVisible(true);
}
if(k==1){this.k=1;
JLabel cel=new JLabel("celsius");JLabel fah=new JLabel("fahrenheit");
this.a=a;this.b=b;
this.c=c;c.setText("convert  ");b.setEditable(true);
setLayout(new FlowLayout());setVisible(true);
}
}

public static void main(String...args){int k;
String chc=JOptionPane.showInputDialog(null,"from C of F");
if(chc.equalsIgnoreCase("c")){
k=0;
}
else k=1;

JTextField a  = new JTextField("                           ");
JTextField b = new JTextField("                            ");
JButton c=new JButton(	);
a.setText("                                                  ");        b.setText("                                                  ");

A aa=new A( a,b,c,k );

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(k==0){
b.setText(null);
double C=Double.parseDouble(a.getText());
double F=180*C/100.+32;
b.setText(Double.toString(F));

}
/*double C=Double.parseDouble(e.getActionCommand());
double F=180*C/100.+32;
b.setText(Double.toString(F));*/
// TODO Auto-generated method stub
if(k==1){
b.setText(null);
double F=Double.parseDouble(a.getText());
double C=((F-32)/180.)*100;//18*C/100.+32;
String dun=new String((Double.toString(C)));

dun=dun.substring(0,dun.length());
b.setText(dun);
}

}}```

#### Comments on this post

• Aurum84 disagrees : Code is not compiling. Please provide proper solutions in future threads.
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
74
Rep Power
22
The problems you encountered were most likely due to the semicolons after your if-statements.
Also you can remove the question for another conversion out of the if-statements: it's code duplication.
You should look at InputMismatchExceptions. When the program asks for a temperature to be converted, you can also put in text.
What is the unit of the temperature the user is converting from?
Here is some edited code to continue with:

Code:
```import java.util.Scanner;

public class Four3
{
public static void main(String[] args)
{
System.out.println("This program will perform a temperature conversion for you.");
double degreesC, degreesF, temp;

Scanner keyboard = new Scanner(System.in);

do
{
System.out.println();
System.out.println("Enter a temperature to be converted:");
temp = keyboard.nextDouble();
System.out.println("Convert this temperature to C or F?");
tempType = keyboard.next();

if (tempType.equalsIgnoreCase("c"))
{
degreesC = (temp - 32) * 5 / 9;
System.out.println(degreesC);
}
else if (tempType.equalsIgnoreCase("f"))
{
degreesF = temp * (9 / 5) + 32;
System.out.println(degreesF);
}
System.out.println("Would you like another conversion?");

}
}```
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Sorry for the Earlier Code. I do not know why it got warped as I copy pasted.

Code:
```package snippet;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class A extends JFrame implements ActionListener{
JTextField a;JTextField b;JButton c	;int i;int k;
public A(JTextField a,JTextField b,JButton c, int k){
i++;System.out.println(i);
//JFrame j=new JFrame();
if(k==0){this.k=0;
JLabel cel=new JLabel("CELSIUS");JLabel fah=new JLabel("FAHRENHEIT");
this.a=a;this.b=b;
this.c=c;c.setText("convert meah");b.setEditable(false);
setLayout(new FlowLayout());setVisible(true);
}
if(k==1){this.k=1;
JLabel cel=new JLabel("celsius");JLabel fah=new JLabel("fahrenheit");
this.a=a;this.b=b;
this.c=c;c.setText("convert  ");b.setEditable(true);
setLayout(new FlowLayout());setVisible(true);
}
}

public static void main(String...args){int k;
String chc=JOptionPane.showInputDialog(null,"from C of F");
if(chc.equalsIgnoreCase("c")){
k=0;
}
else k=1;

JTextField a  = new JTextField();
JTextField b = new JTextField("                           ");
JButton c=new JButton(	);
a.setText("         ");
A aa=new A( a,b,c,k );

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(k==0){
b.setText(null);
double C=Double.parseDouble(a.getText());
double F=180*C/100.+32;
b.setText(Double.toString(F));

}
/*double C=Double.parseDouble(e.getActionCommand());
double F=180*C/100.+32;
b.setText(Double.toString(F));*/
// TODO Auto-generated method stub
if(k==1){
b.setText(null);
double F=Double.parseDouble(a.getText());
double C=((F-32)/180.)*100;//18*C/100.+32;
String dun=new String((Double.toString(C))).substring(0, 5);

b.setText(dun);
}

}}```