Hi, there, very new to Java. I'm looking to get a number from a text box, then use the number in a calculation, and then put the answer into another text box. JButton convertButton = new JButton ("Convert to Fahrenheit");
final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
final JTextField fahrenheitText = new JTextField (" ");
JPanel buttons = new JPanel();
buttons.setLayout (new FlowLayout());
buttons.add (convertButton);
setTitle("Doubler Frame");
setLayout(new BorderLayout());
add(fahrenheitText, BorderLayout.SOUTH);
add(buttons, BorderLayout.CENTER);
add(celsiusText, BorderLayout.NORTH);
convertButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
fahrenheit = (9/5)* + 33.8;
fahrenheitText.setText(Temperature in Fahrenheit: " + fahrenheit);
}
}); This is the main part of the code, I'm very confused and can't figure out whats wrong.
Hope you guys can spot my mistake.
I get an answer of 33.8, no matter what I enter in the first text box.
Would much appreciate help. Thanks! 6 3257 Hi, there, very new to Java. I'm looking to get a number from a text box, then use the number in a calculation, and then put the answer into another text box. JButton convertButton = new JButton ("Convert to Fahrenheit");
final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
final JTextField fahrenheitText = new JTextField (" ");
JPanel buttons = new JPanel();
buttons.setLayout (new FlowLayout());
buttons.add (convertButton);
setTitle("Doubler Frame");
setLayout(new BorderLayout());
add(fahrenheitText, BorderLayout.SOUTH);
add(buttons, BorderLayout.CENTER);
add(celsiusText, BorderLayout.NORTH);
convertButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
fahrenheit = (9/5)* + 33.8;
fahrenheitText.setText(Temperature in Fahrenheit: " + fahrenheit);
}
}); This is the main part of the code, I'm very confused and can't figure out whats wrong.
Hope you guys can spot my mistake.
I get an answer of 33.8, no matter what I enter in the first text box.
Would much appreciate help. Thanks!
If (9/5) is A
and 33.8 is B
the formula is A * celsiusvalue + B
since celsiusvalue is 0... A * celsiusvalue always returns 0...
That is why 33.8.....
1. Please post your code with codetags.
2. Someone may attempt to test your code, but your code is not complete....
3. You forgot to set the size of the frame.
4. fahrenheit is what Data type?
5. Having the input String to Double
- Get the String value of celsiusText
- Use Double.parseDouble(String) for Double Conversion.... or float
- Add the value into your formula....
6. Download the Java Core API... It is useful...
7. Good luck.
sukatoa...
Ok this is my code in full now: import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class Temperature {
public static void main(String[] args) {
tempConvert buttonFrame = new tempConvert();
buttonFrame.setSize(350,120);
buttonFrame.setLocation(250,100);
buttonFrame.setVisible(true);
}
}
class tempConvert extends JFrame{
//Stores default temperature numbers
double celsius = 1;
double fahrenheit = 33.8;
public tempConvert ()
{
JButton convertButton = new JButton ("Convert to Fahrenheit");
final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
final JTextField fahrenheitText = new JTextField (" ");
JPanel buttons = new JPanel();
buttons.setLayout (new FlowLayout());
buttons.add (convertButton);
//creates layout of window
setTitle("Doubler Frame");
setLayout(new BorderLayout());
add(fahrenheitText, BorderLayout.SOUTH);
add(buttons, BorderLayout.CENTER);
add(celsiusText, BorderLayout.NORTH);
//states what to do when button is pressed
convertButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
celsius = Double.parseDouble (celsiusText);
fahrenheit = (9/5)* + 33.8;
fahrenheitText.setText("Fahrenheit is: " + fahrenheit);
}
});
}
} and I still get "Error: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (javax.swing.JTextField)"
I don't know if I'm using the parse correctly...
Ok this is my code in full now: import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class Temperature {
public static void main(String[] args) {
tempConvert buttonFrame = new tempConvert();
buttonFrame.setSize(350,120);
buttonFrame.setLocation(250,100);
buttonFrame.setVisible(true);
}
}
class tempConvert extends JFrame{
//Stores default temperature numbers
double celsius = 1;
double fahrenheit = 33.8;
public tempConvert ()
{
JButton convertButton = new JButton ("Convert to Fahrenheit");
final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
final JTextField fahrenheitText = new JTextField (" ");
JPanel buttons = new JPanel();
buttons.setLayout (new FlowLayout());
buttons.add (convertButton);
//creates layout of window
setTitle("Doubler Frame");
setLayout(new BorderLayout());
add(fahrenheitText, BorderLayout.SOUTH);
add(buttons, BorderLayout.CENTER);
add(celsiusText, BorderLayout.NORTH);
//states what to do when button is pressed
convertButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
celsius = Double.parseDouble (celsiusText);
fahrenheit = (9/5)* + 33.8;
fahrenheitText.setText("Fahrenheit is: " + fahrenheit);
}
});
}
} and I still get "Error: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (javax.swing.JTextField)"
I don't know if I'm using the parse correctly...
parseDouble needs a parameter String....
That string maybe the string value of textfield......
textfield.getText().... read about it....
That String will be used to convert the String into Double....
That Double will then be returned after the process.....
Where must the celsius be on the formula? hint...
The complete explaination is on the web....
you can google it.
sukatoa
parseDouble needs a parameter String....
That string maybe the string value of textfield......
textfield.getText().... read about it....
That String will be used to convert the String into Double....
That Double will then be returned after the process.....
Where must the celsius be on the formula? hint...
The complete explaination is on the web....
you can google it.
sukatoa
hmmm... still cant get the String from the textbox, doesn't matter though mate, might just try it using an input box or something, I'm sure I'll make sense of it all on day, and thanks for the help anyway man, appreciate it!
Take care
That constant 33.8 in your (linear) conversion formula is incorrect:
F = 9/5*C+32
C = 5/9*(F-32)
kind regards,
Jos
That constant 33.8 in your (linear) conversion formula is incorrect:
F = 9/5*C+32
C = 5/9*(F-32)
kind regards,
Jos
I've given up trying to guess where the OP got 33.8 from.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: LRW |
last post by:
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:
$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS...
|
by: Klaus Bonadt |
last post by:
In order to protect software from being copied without licence, I would like
to use something like a key, which fits only to the current system. The
serial number of the CPU or the current...
|
by: stuart |
last post by:
I am trying to format a number in an unbound text field using the
following format statement in the field's Exit event:
txt11UnitWeight = Format(txt11UnitWeight, "00000.00")
I want the number...
|
by: shumaker |
last post by:
I see other posts where some say fields that will hold a number with
leading zeros should be stored as text instead of numbers.
This is very inefficient though, as a string of digit characters...
|
by: Vasilis Serghi |
last post by:
Presently I define the number of lines to be expected in a file when
defining the array size and the initialisation of this array. This works
fine for now, but i'm sure that in the future this...
|
by: venu |
last post by:
Hi,
I have a different requirement and it is :
I need to validate a phone number field.
It may or may not be a US phone number.
The constraints are :
***********************
# It should...
|
by: Jean-François Michaud |
last post by:
Hello all,
I need to generate (SHEET X of Y) in titles for figures contained in a
section (I already generate page A of B at the bottom right of every
page in the document).
Is there a way...
|
by: brad.goldberg |
last post by:
2 Part question for you all, seems kinda tricky to me, but many of you
are access gods compared to myself.
Here's the deal... I have a label on my form called Run Number:
Next to it I have a...
|
by: Orchid |
last post by:
How can I change a Date datatype to a Number datatype? For example, I
want a date 10/31/2006 to show 1031 as Number datatype. But I don't
want it becomes 39021. What formula should I use? ...
|
by: Frinavale |
last post by:
Filtering user input is extremely important for web programming.
If input is left unfiltered users can input malicious code that can cripple your website.
This article will explain how to make...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |