6

I am brand new to Java, started two weeks ago and am having issues wrapping my mind around this issue. I have a problem in a class I am taking that has been brought up before. Converting kilograms to pounds and rounding to the second decimal place.

I can create the input side of things and bring up a dialog box to prompt the user to enter in a weight. I can also create an output that uses an equation I made to output the answer in a dialog box.

My question is how do I take the information that is input and use it to convert from kilograms to pounds?

I have been reading my book and scouring the internet trying to find an answer and I think I may be over thinking it. Thanks for the help.

Input.java:

//This program asks a user to input a weight in kilograms.

package module2;

import javax.swing.*;

public class Input {

    public static void main(String[] args) {

        String weight;

        weight = JOptionPane.showInputDialog("Enter weight in kilograms");
    }
}

Output.java:

//This program outputs a converted weight from kilograms to pounds.

package module2;

import javax.swing.JOptionPane;

public class Output {

    public static void main(String[] args) {

        double kg = 75.5;
        double lb = 2.2;
        double sum;

        sum = (kg * lb);

        JOptionPane.showMessageDialog(null,sum, "Weight Conversion", JOptionPane.INFORMATION_MESSAGE);
    }
}
Obicere
  • 2,999
  • 3
  • 20
  • 31
Ph1shPhryd
  • 109
  • 7
  • Well for starter you can't have 2 main methods. – RichN May 20 '15 at 19:03
  • @RichN you can, but only one would be ran per instance. You could always invoke the other, but that is a bad thing to do. – Obicere May 20 '15 at 19:04
  • 1
    @Obicere Ah yes of course. I was assuming the "normal" way of using `main` methods. And besides, if you're knowledgeable enough to do that, you probably wouldn't have written them as `main` methods. – RichN May 20 '15 at 19:10

2 Answers2

6

Right now you have 2 main methods. Both of these are entry points for the program. Since they have to share information, it doesn't make sense you have both.

What I'd recommend is to change the Output's main method to a instance method, taking one parameter: the weight from the Input.

Like so:

public void printOutput(final double weight){
    //...
}

You can then call that from the Input's main method like so:

public static void main(String[] args) {

    String weight;

    weight = JOptionPane.showInputDialog("Enter weight in kilograms");

    double kg = Double.parseDouble(weight); // Be sure to parse the weight to a number
    Output output = new Output(); // Create a new instance of the Output class

    output.printOutput(kg); // Call our method to display the output of the conversion
}

One other thing, is that since Output is currently only used for that one method, you could consider just moving that method into Input.

Obicere
  • 2,999
  • 3
  • 20
  • 31
  • Thank you so much for the help. This Java stuff is a lot of take in and sort out. What does it mean when you say Double.parseDouble(weight) specifically the parseDouble? Once again, thank you for your help. – Ph1shPhryd May 20 '15 at 19:14
  • @Ph1shPhryd `weight` is a `String`. Just a list of characters. You can't really add or multiply or divide with that as if it were a number. Instead, you can call upon the Java library to take that string and parse it. This then returns a `double`, which is a number. So you can then use it as such. – Obicere May 20 '15 at 19:19
  • that makes sense. So when I run the program now the answer always stays the same no matter what I input. I am trying to figure out the issue but seem to be stuck. Any ideas as to why that is happening? – Ph1shPhryd May 20 '15 at 19:23
  • @Ph1shPhryd most likely you're still using the values initially set to `kg` instead of the parameter `weight`. – Obicere May 20 '15 at 19:24
  • Holy cow, it works! Do you have any tips to work through issues as a beginner using Java @Obicere? When I am reading the book I understand what I need to do and when I think about the problem away from the computer I know what needs to be done but when I sit in front of the computer I lose everything. – Ph1shPhryd May 20 '15 at 19:32
  • 1
    @Ph1shPhryd usually breaking it down to smaller discrete problems help. For example with this one: 1. read weight as double from user, 2. perform the conversion, 3. display output of result to user. This way you can focus on a single part of a program, test it out fully and really limit opportunities for bugs. – Obicere May 20 '15 at 19:42
0
// addition of two integers using JOptionPane
import javax.swing.JOptionPane; 

public class Addition

{

   public static void main(String[] args)

  {

    String firstNumber = JOptionPane.showInputDialog("Input <First Integer>");
    String secondNumber = JOptionPane.showInputDialog("Input <Second Integer>");
    int num1 = Integer.parseInt(firstNumber);
    int num2 = Integer.parseInt(secondNumber);
    int sum = num1 + num2;
    JOptionPane.showMessageDialog(null, "Sum is" + sum, "Sumof two Integers", JOptionPane.PLAIN_MESSAGE);
  }
}
RomCoo
  • 1,868
  • 2
  • 23
  • 36
MyStack
  • 1
  • 2