# im new and need help

Page 1 of 1

## 9 Replies - 968 Views - Last Post: 22 June 2009 - 07:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=111149&amp;s=d4ef5c4ca061b35dc5876d0a8e7bad17&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Java_noob

Reputation: 0
• Posts: 9
• Joined: 21-June 09

# im new and need help

Posted 21 June 2009 - 11:08 AM

i need to write a program which generates 3 random numbers called tilfældigtal1, tilfældigtal2 and tilfældigtal 3.

with these 3 numbers it must find the sum and the mean number (the number you find, when you find the sum of the 3 numbers and / it with 3) .

my code looks like this, but i dont know whats wrong with it.

public class sum_and_mean_number {

public static void main(String[] args) {

double tilfældigtal1, tilfældigtal2, tilfældigtal3;
tilfældigtal1 = Math.random();
tilfældigtal2 = Math.random();
tilfældigtal3 = Math.random();

System.out.println( "1 tal er"+tilfældigtal1", 2 tal er"+tilfældigtal2" og 3 tal er "+tilfældigtal3" );

double Mean, Sum;
Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3);
Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

System.out.println("sum is "+Sum" og Mean is "+Mean);
}
}

can you plz explain where i did something wrong, and why it was.

since i must know that, so i dont make a mistake again

This post has been edited by Java_noob: 21 June 2009 - 11:09 AM

Is This A Good Question/Topic? 0

## Replies To: im new and need help

### #2 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: im new and need help

Posted 21 June 2009 - 11:23 AM

tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3);

Your dividing doubles by an integer, which is giving you integer division. So you need to divide by a double, that is still 3. 3.0 <- change to that.

### #3 Java_noob

Reputation: 0
• Posts: 9
• Joined: 21-June 09

## Re: im new and need help

Posted 21 June 2009 - 11:45 AM

public class Sum_and_mean_number {

public static void main(String[] args) {

double tilfældigtal1, tilfældigtal2, tilfældigtal3;
tilfældigtal1 = Math.random();
tilfældigtal2 = Math.random();
tilfældigtal3 = Math.random();

System.out.println( "1 tal er"+tilfældigtal1", 2 tal er"+tilfældigtal2" og 3 tal er "+tilfældigtal3" );

double Mean, Sum;
Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3.0);
Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

System.out.println("sum is "+Sum" og Mean is "+Mean);
}
}


like this ?

it still wont work

### #4 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: im new and need help

Posted 21 June 2009 - 11:48 AM

What isnt working? What are your errors?

### #5 Java_noob

Reputation: 0
• Posts: 9
• Joined: 21-June 09

## Re: im new and need help

Posted 21 June 2009 - 11:53 AM

--------------------Configuration: Sum_and_mean_number - JDK version <Default> - <Default>--------------------
java.lang.NoClassDefFoundError: Sum_and_mean_number
Caused by: java.lang.ClassNotFoundException: Sum_and_mean_number
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
Could not find the main class: Sum_and_mean_number. Program will exit.
Process completed.

help me plz

### #6 mobius4139

Reputation: 3
• Posts: 118
• Joined: 24-March 09

## Re: im new and need help

Posted 21 June 2009 - 11:54 AM

Try baby stepping the program and replacing the random numbers with actual numbers for testing purposes. This will help the process of elimination for finding the actual problem.
by doing this you can determine if the problem lies with your random numbers generator or with your figuring the mean and sum.

~Bruce

### #7 Java_noob

Reputation: 0
• Posts: 9
• Joined: 21-June 09

## Re: im new and need help

Posted 21 June 2009 - 12:06 PM

 public class Sum {

public static void main(String[] args) {

System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);

double Mean, Sum;
Mean = ((2 + 4 + 3) / 3.0);
Sum = (2 + 4 + 3);

System.out.println("sum is "+Sum" og Mean is "+Mean);
}
}


it still wont work when i do this.

error 1 says this

--------------------Configuration: <Default>--------------------
C:\prog\sum.java:14: ')' expected
System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
^
C:\prog\sum.java:14: not a statement
System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
^
C:\prog\sum.java:14: ';' expected
System.out.println( "1 tal er"+2", 2 tal er"+4" og 3 tal er "+3);
^
C:\prog\sum.java:20: ')' expected
System.out.println("sum is "+Sum" og Mean is "+Mean);
^
C:\prog\sum.java:20: not a statement
System.out.println("sum is "+Sum" og Mean is "+Mean);
^
C:\prog\sum.java:20: ';' expected
System.out.println("sum is "+Sum" og Mean is "+Mean);
^
6 errors
Process completed.



and error 2 says this
--------------------Configuration: Sum_and_mean_number - JDK version  <Default> - <Default>--------------------
java.lang.NoClassDefFoundError: Sum_and_mean_number
Caused by: java.lang.ClassNotFoundException: Sum_and_mean_number
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
Could not find the main class: Sum_and_mean_number.  Program will exit.
Process completed.



### #8 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: im new and need help

Posted 21 June 2009 - 12:30 PM

After you add the variables you need to put another '+'

"test" +Sum + " text";

Make sure the class name is the file name. Case sensitive

### #9 tanah.melayu

Reputation: 5
• Posts: 45
• Joined: 21-February 09

## Re: im new and need help

Posted 21 June 2009 - 05:06 PM

Fuzzyness, on 21 Jun, 2009 - 11:30 AM, said:

After you add the variables you need to put another '+'

"test" +Sum + " text";

Make sure the class name is the file name. Case sensitive

yup! he's true... i already fix your source code and this should work. Fuzzyness said it should look like this:

public class Sum_and_mean_number {

public static void main(String[] args) {

double tilfældigtal1, tilfældigtal2, tilfældigtal3;
tilfældigtal1 = Math.random();
tilfældigtal2 = Math.random();
tilfældigtal3 = Math.random();

System.out.println( "1 tal er" + tilfældigtal1 + ", 2 tal er" + tilfældigtal2 + " og 3 tal er " + tilfældigtal3 );

double Mean, Sum;
Mean = ((tilfældigtal1 + tilfældigtal2 + tilfældigtal3)/3.0);
Sum = (tilfældigtal1 + tilfældigtal2 + tilfældigtal3);

System.out.println("sum is " + Sum + " og Mean is " + Mean);
}
}


the biggest mistake is u miss the '+' symbol. whenever u found errors, try to understand each of them. one day, u will able to read them and know what is actually going wrong.

~Happy Coding~

### #10 Java_noob

Reputation: 0
• Posts: 9
• Joined: 21-June 09

## Re: im new and need help

Posted 22 June 2009 - 07:29 AM

Thanks