Objects

Ive tried for hours to get this to compile correctly.

Either i recieve errors regarding the "private static String getName()" method.

or

none of my dialog boxes shows up when i run this program

Can someone point out my mistake that im not seeing?

Please

import javax.swing.JOptionPane;

publicclass Swimmer{

publicstaticvoid main (String [] args){

String swimmerName;

double initTime;

double finalTime;

double bestTime = 0;

swimmerName = getName();

initTime = getInitTime();

finalTime = getFinTime();

bestTime = getBest(initTime, finalTime);

}

}

privatestatic String getName()

{

String name;

name = JOptionPane.showInputDialog(null,"Enter swimmers name");

return name;

}

publicdouble getInitTime()

{

double initialT;

initialT = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter initial time"));

return initialT;

}

publicdouble getFinTime()

{

double finalT;

finalT = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter final time"));

return finalT;

}

publicdouble getBest(double initTime,double finalTime)

{

initTime = 0;

finalTime = 0;

double bestTime = 0;

if (finalTime > initTime)

{

bestTime = initTime;

}

else

{

if (initTime > finalTime)

{

bestTime = finalTime;

}

}

return bestTime;

}

publicvoid output()

{

outString += swimmerName +"\n","Best Time: " + bestTime +" seconds\n";

}

}

[3451 byte] By [kumiko2ua] at [2007-11-27 1:01:24]
# 1
For starters, your braces don't match. There are two instead of oneat the end of you main method:bestTime = getBest(initTime, finalTime);}}
DrLaszloJamfa at 2007-7-11 23:36:16 > top of Java-index,Java Essentials,New To Java...
# 2

The first errors that I got while compiling your code are related to the use of non-static methods from a static context.

The main method runs in a static context and in Java you can't make a static reference to a non-static method from a static context. So you either declare the methods getInitTime, getFinTime, getBest as static or in your main you declare an instance variable like:

Swimmer main = new Swimmer();

main.getInitTime();

...etc.

mtedonea at 2007-7-11 23:36:16 > top of Java-index,Java Essentials,New To Java...
# 3

This is what i have so far. only errors im receiving now is in my output method

import javax.swing.JOptionPane;

public class Swimmer

{

private String SwimmerName;

private double initTime;

private double finalTime;

private double bestTime = 0;

public String getName()

{

String name;

name = JOptionPane.showInputDialog(null,"Input swimmer's name");

return name;

}

public double getInitTime()

{

double initialT;

initialT = Double.parseDouble(

JOptionPane.showInputDialog(null, "Enter initial time"));

return initialT;

}

public double getFinTime()

{

double finalT;

finalT = Double.parseDouble(

JOptionPane.showInputDialog(null, "Enter final time"));

return finalT;

}

public double getBest(double initTime, double finalTime)

{

initTime = 0;

finalTime = 0;

double bestTime = 0;

if (finalTime > initTime)

{

bestTime = initTime;

}

else

{

if (initTime > finalTime)

{

bestTime = finalTime;

}

}

return bestTime;

}

public void output()

{

outString += swimmerName + "\n";

outString += "Best Time: " + bestTime + " seconds\n";

JOptionPane.showMessageDialog(null, outString);

}

}

kumiko2ua at 2007-7-11 23:36:16 > top of Java-index,Java Essentials,New To Java...
# 4

That is because the variable outString hasn't been declared and therefore the compiler can't resolve it.

Change the line:

outString += swimmerName + "\n";

to

String outString = "";

outString += SwimmerName + "\n";//Note that you also mispelled SwimmerName as swimmerName

The code will now compile but note that you didn't assign a value to the SwimmerName variable and the compiler assigned a default value to it, which is the null value since this is a reference variable, so be careful.

mtedonea at 2007-7-11 23:36:16 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks!It compiles fine, thanks to youBut the dialog boxes still wont pop up :*(
kumiko2ua at 2007-7-11 23:36:16 > top of Java-index,Java Essentials,New To Java...