9 Replies - 2170 Views - Last Post: 28 September 2010 - 09:43 PM Rate Topic: -----

#1 andyluvskrissy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 10

Questions on what exactly does the code mean.

Posted 28 September 2010 - 06:59 PM

import java.util.*;
import java.text.*;
import javax.swing.*;
class Lab4{
   public static void main (String[] args) {;
		String tempF;
		double num, F;
		JFrame myWindow=new JFrame();
		mywindow.setSize(300,300);
		mywindow.setTitle("Calculator");
		mywindow.setVisible(true);
		mywindow.setLocation(400, 100);
		tempF=JOptionPane.showInputDialog(myWindow, "Enter temp...");
		F=125;
		num=((5.0/9.0)*(F-32));
		JOptionPane.showMessageDialog(myWindow, "The temp is "+num);

		}
}



Currently I'm taking basic programming class in College "Object Oriented Programming" using JGrasp. My teacher want us to create a program where it will ask the user to enter current temperature in Fahrenheit and then convert it to degrees Celsius using the formula C=5/9*(F-32). However I need to display the converted temperature using a dialog box. I got to this part and I'm totally confused as to what to do next or for that matter what I'm doing wrong. My problem is how can I define the variable F so you can put any number in and it would convert it into degrees Celsius. I tried "Scanner scanner=new Scanner(System.in);" and then "F=scanner.nextDouble();" and that didn't work. I believe I have to use the parseDouble() method or parseInt() method or simply converting string to Int but I have no clue therefore I just need help to what exactly am I doing wrong thank you for helping.

Is This A Good Question/Topic? 0
  • +

Replies To: Questions on what exactly does the code mean.

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 07:35 PM

The string value of tempF returned by JOptionPane.showInputDialog() needs to be converted to a double. The Double Class has a method that will attempt to convert the string into a double: parseDouble(). Because the string might not be convertible, the method can throw an exception. To handle this possibility, the call to the method needs to be enclosed in a try...catch block:

...
tempF = JOptionPane.showInputDialog(myWindow, "Enter temp...");

try {
    F = Double.parseDouble(tempF.trim());
    // trim removes any leading or trailing whitespace
}
catch (Exception ex){
    System.out.println(ex.toString());
}

num = ((5.0 / 9.0) * (F - 32));
...



To use the Double method change double num, F; to
Double num, F = 0.; Before this change num, F are primitives.


You have some typos: mywindow should be myWindow

This post has been edited by n8wxs: 28 September 2010 - 07:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 andyluvskrissy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 10

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 07:48 PM

import java.util.*;
import java.text.*;
import javax.swing.*;
class Lab4{
   public static void main (String[] args) {;
		String tempF;
		double num, F;
		JFrame myWindow=new JFrame();
		mywindow.setSize(300,300);
		mywindow.setTitle("Calculator");
		mywindow.setVisible(true);
		mywindow.setLocation(400, 100);
		tempF=JOptionPane.showInputDialog(myWindow, "Enter temp...");
		try {
	   F=Double.parseDouble(tempF.trim());
	}
		catch (Exception ex){
	   System.out.println(ex.toString());
	}
		F=120;
		num=((5.0/9.0)*(F-32));
		JOptionPane.showMessageDialog(myWindow, "The temp is "+num);
	}
}



I tried the code however I still need help with when the Dialog box asks me for an input of F which is Fahrenheit. How can the code convert it into Celsius as I have used the "Scanner scanner=new Scanner(System.in);" and then "F=scanner.nextDouble();" and that didn't work.

This post has been edited by andyluvskrissy18: 28 September 2010 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 07:58 PM

Remove this line:

F=120;


because it is being set already:

F=Double.parseDouble(tempF.trim());

This post has been edited by n8wxs: 28 September 2010 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 andyluvskrissy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 10

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 08:16 PM

View Postn8wxs, on 28 September 2010 - 06:58 PM, said:

Remove this line:

F=120;


because it is being set already:

F=Double.parseDouble(tempF.trim());


When i remove:

F=120;


and then I try compiling it, it gives me an error of:

Lab4.java:20: variable F might not have been initialized
num=((5.0/9.0)*(F-32));
Was This Post Helpful? 0
  • +
  • -

#6 andyluvskrissy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 10

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 08:25 PM

Nevermind I got it thanks a lot for helping but one last question. All the new things you showed me can you explain what each means or does as I want to learn it and understand it as well. Thanks for helping.

Double num, F = 0.;




and

	
try {
F=Double.parseDouble(tempF.trim());
}
catch (Exception ex){
System.out.println(ex.toString());
}



Was This Post Helpful? 0
  • +
  • -

#7 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 08:27 PM

Well you should look at the Double link I gave you. Java has 'wrappers' for the primitive types: integer and double. The classes have methods that make converting numbers easier.


Here's a reference for primitives: Primitive Data Types

To learn about the try...catch exception handling, have a look at What Is an Exception?

This post has been edited by n8wxs: 28 September 2010 - 08:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 andyluvskrissy18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 10

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 08:36 PM

import java.util.*;
import java.text.*;
import javax.swing.*;
class Lab4{
   public static void main (String[] args) {;
		String tempF;
		Double num, F = 0.;
		JFrame myWindow=new JFrame();
		mywindow.setSize(500,400);
		mywindow.setTitle("Calculator");
		mywindow.setVisible(true);
		mywindow.setLocation(600,200);
		tempF=JOptionPane.showInputDialog(myWindow, "What is the current temperature in F?");
		try {
	   F=Double.parseDouble(tempF.trim());
		}
		catch (Exception ex){
	   System.out.println(ex.toString());
		}
		num=((5.0/9.0)*(F-32));
		JOptionPane.showMessageDialog(myWindow, "The temperature in C is "+num);
	}
}



Recently I had some help to create this program however there are parts of this code where I don't know what the function or portions of the code in what it does to the overall program. The two parts are:

try {
F=Double.parseDouble(tempF.trim());
}
catch (Exception ex){
System.out.println(ex.toString());
}


and
Double num, F = 0.;


These two codes in my programs are what is troubling me as I don't know what it does to the overall program. Also overall this program is a calculator to convert a given degrees Fahrenheit into degrees Celsius. Thanks for helping.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 09:28 PM

Double num, F = 0.;



That is a Double object, quite like the double datatype except that it has methods and such as well.

Alright, let's look at this segment:
try {
    F=Double.parseDouble(tempF.trim());
}
catch (Exception ex){
    System.out.println(ex.toString());
}



The try { } catch { } of course handles any errors. But the F = Double.parseDouble(tempF.trim()); part takes a String (tempF), gets rid of extra spaces (trim()), and then converts that String into a Double.
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Questions on what exactly does the code mean.

Posted 28 September 2010 - 09:43 PM

Actually

double num, F = 0.;
will do the same job as
Double num, F = 0.;
with less overhead

Duplicated topics merged
Please avoid double posting
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1