highest number

hi!

I'm trying to get this program code to display the highest of the 3 numbers that the user has to enter, but i don't know what's wrong with it. can any1 help?

import java.io.*;

publicclass maxnumber

{

publicstaticvoid main(String [] args)throws IOException

{

int num1, num2, num3, max0;

BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter 3 numbers");

System.out.println("The highest number will be displayed at the end");

System.out.println("First Number: ");

String input = reader.readLine();

num1 = Integer.parseInt(input);

System.out.print("Second Number: ");

input = reader.readLine();

num2 = Integer.parseInt(input);

System.out.print("Third Number: ");

input = reader.readLine();

num3 = Integer.parseInt(input);

num1 = max0;

if (num2 > num1) max0 = num2;

elseif (num3 > num2) max0 = num3;

System.out.println("The maximum number is " +max0);

}

}

javac-d . -g -nowarn maxnumber.java

maxnumber.java:27: variable max0 might not have been initialized

num1 = max0;

^

1 error

[1923 byte] By [mark_8206a] at [2007-11-26 14:49:43]
# 1

> num1 = max0;

Take out that line as it doesnt' achieve anything

>

> if (num2 > num1) max0 = num2;

> else if (num3 > num2) max0 = num3;

>

And take out the "else" as your code won't work when num3 > num2 AND num2 > num1.

Ted.

ted_trippina at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 2

ted, my code is now what appears below but i'm still gettin gthe same error message.

import java.io.*;

public class maxnumber

{

public static void main(String [] args) throws IOException

{

int num1, num2, num3, max0;

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter 3 numbers");

System.out.println("The highest number will be displayed at the end");

System.out.println("First Number: ");

String input = reader.readLine();

num1 = Integer.parseInt(input);

System.out.print("Second Number: ");

input = reader.readLine();

num2 = Integer.parseInt(input);

System.out.print("Third Number: ");

input = reader.readLine();

num3 = Integer.parseInt(input);

if (num2 > num1) max0 = num2;

if (num3 > num2) max0 = num3;

System.out.println("The maximum number is " +max0);

}

}

mark_8206a at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 3
what happens in that code if the numbers are:num1 = 3num2 = 2num3 = 1this explains your error message.
prob.not.sola at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 4
int a = 7;int b = 2;int c = 12;System.out.println(Math.max(Math.max(a, b), c));
kajbja at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 5
> maxnumber.java:27: variable max0 might not have been> initialized> num1 = max0;>^As the error message states, you have to initialize variables before you can use them. In your code, variable "max0" has not been initialized.
tjjalavaa at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 6
How do i initialize it?
mark_8206a at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 7
> How do i initialize it?set it to num1 before your if statements.
kajbja at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 8
it works, thanks
mark_8206a at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 9
Good day friendsyou are trying to asssign like this num1=max0variable max0 value is not yet assignedhow we can a assign value to already um- assign varibethats why u got probelmwth regardsvasu_gb@yahooo.co,in
vasu_gba at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 10
Sorry...Accidentally posted....Sorry for the disturbanceMessage was edited by: shagil
shagila at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 11
vasu_gb and shagil, what did you not understand about reply #8? Note that reply #8 is from the original poster of this thread (and from yesterday!) saying that s/he solved the problem.Edit:Ah, I see shagil editted his reply.; )
prometheuzza at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...
# 12

Good day friends

you are trying to asssign like this

num1=max0

variable max0 value is not yet assigned

<insert useless advice that probably doesnt make sense or won't work or is just plain wrong>

Listening to me? Well, thats why u got probelm

wth regards

vasu_gb@still.learning.but.trying.to.advise.even.though.dont.know.what.on.about

ted_trippina at 2007-7-8 8:37:45 > top of Java-index,Java Essentials,New To Java...