English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writing this in JCreator and trying to get the output to display Hello World! The kilometer equivalent of 42.2 miles is 67.91457. can anyone help?

public class HelloWorld
{
public static void main (String []args)

{
Double Mile = 42.2;
Final Double Conversionfactor = 1.60935;
Double km = Double Mile * Final Double Conversionfactor;
System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + Double km + ".");
}
}// end class

2006-10-02 23:11:58 · 9 answers · asked by Princess Peach 3 in Computers & Internet Programming & Design

This is Java Language

2006-10-02 23:15:19 · update #1

If i was clever i would have spotted what was wrong with my code! Im first year undergraduate studying computer science and am only starting to learn Java programming!

2006-10-02 23:23:47 · update #2

9 answers

At first glance, I'd say your syntax is the issue here. You declare your variables as

Double Mile
Final Double

and so forth.

Java does not consider these as valid.

Types such as double must be written in lower case all the way.

Try this one here:

public class HelloWorld
{
public static void main (String []args)

{
double Mile = 42.2;
final double Conversionfactor = 1.60935;
double km = Mile * Conversionfactor;
System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + km + ".");
}
}// end class

Look throught this code and see what changes I've made. Hope that helps.

2006-10-02 23:17:51 · answer #1 · answered by paidpaipa 2 · 2 0

You're nearly there - what you need it to have a constructor in this class, and then use the main method to call the constructor, so your code should read:

public class HelloWorld {
// call constructor
public HelloWorld() {
Double Mile = 42.2;
Final Double Conversionfactor = 1.60935;
Double km = Mile * Conversionfactor;
System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + km + ".");
}

// call main method
public static void main (String []args){
new HelloWorld();
}
}// end class

I have removed the variable declaration's out of your code - e.g. Double, Final etc. as they are only needed when you first create the variable - thereafter, just reference Conversionfactor as Converstionfactor as opposed to Final Double Conversionfactor. Also, variables shouldn't start with a capital letter. Try renaming Converstionfactor to conversionFactor and Mile to mile.

Good luck! It'll come right in the end!

2006-10-03 11:43:58 · answer #2 · answered by Stewie Griffin 2 · 0 0

Here we go

Java is case sensitive so Double and Final should be double and final

You also do not need to declare the variable type in the expression e.g. Double km = Double Mile * Final Double Conversionfactor; unless you are casting the value to a different type (The casting syntax is different).

Your code should read:
public static void main(String[] args) {
double Mile = 42.2;
final double Conversionfactor = 1.60935;
double km = Mile * Conversionfactor;
System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + km + ".");
}

2006-10-02 23:22:55 · answer #3 · answered by Paul B 3 · 1 0

you are using Double instead of double and some other mistakes too
just study the following code, it will work

public class HelloWorld
{
public static void main (String []args)

{
final double Mile = 42.2;
final double Conversionfactor = 1.60935;
final double km = Mile * Conversionfactor;
System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + km + ".");
}
}// end class

2006-10-03 01:04:29 · answer #4 · answered by howsureyouare 3 · 0 0

This line:

System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + Double km + ".");

Should read this:

System.out.println("Hello World!\nThe kilometer equivalent of 42 miles is " + km + ".");

I'm not a Java programmer, and have never looked into it, but from my knowledge of PHP and Visual Basic, the "Double" tells it what type of string it is, and then you state the name of it, so:

Double km

Tells it that km is a double integer. When you come to call it, you just put the name. It already knows the type of value it is.

2006-10-02 23:16:02 · answer #5 · answered by Mike 2 · 1 1

Double km = Double Mile * Final Double Conversionfactor;

this statement should be like this

Double km = Double Mile * Conversionfactor;

Everything else is Fine

2006-10-02 23:41:00 · answer #6 · answered by H@ri 2 · 0 0

I'm not a Java expert but what is obvious here is that you surely should not be using your type declarations such as "Double" or "Final Double" in your calculations.

These declarations are only to be used on the Declaration line itself, henceforth you refer to the variable ONLY by its name

eg: double km = Mile * Conversionfactor;

2006-10-02 23:23:36 · answer #7 · answered by Norman 4 · 0 1

This is far too high tech for me.How do you all get to be so clever.

2006-10-02 23:20:49 · answer #8 · answered by Julie 5 · 0 1

Is this moon language or what?

2006-10-02 23:14:24 · answer #9 · answered by Boscombe 4 · 0 3

fedest.com, questions and answers