error explanation

Hi Guys,

I am building a program that calculates the wage of several workers, I decided to have a class WageCalculator that does most of the work and another called InputReader to get the values from the user. This program has a simple text interface.

this is the code for the wage calculator class at this stage:

/Code

public class WageCalculator

{

InputReader inputReader;

private float basicHourlyWage;

private String familyName;

private String firstName;

private String skillGrade;

private int hoursWorked;

int numberOfWeeks;

int[] hoursPerWeek = new int[7];

public WageCalculator()

{

inputReader = new InputReader();

start();

}

//main method

public static void main(String[] args)

{

WageCalculator wageCalculator = new WageCalculator();

}

// start method

private void start()

{

textUI();

}

private void textUI()

{

System.out.print("Basic Hourly Wage:");

basicHourlyWage = InputReader.getFloat();

System.out.print("Family Name:");

familyName = InputReader.getString();

System.out.print("First Name:");

firstName = InputReader.getString();

System.out.print("Skill Grade:");

skillGrade = InputReader.getString();

System.out.print("How many weeks shall I calculate?");

numberOfWeeks = InputReader.getInt();

for (int i = 1; i <= numberOfWeeks; i++)

{

System.out.print("Hours worked in Week" + i + ":");

hoursPerWeek = InputReader.getInt();

}

}

}

/~Code

The errors I get are these:

D:\UNI\JAVA\WageCalculator.java:52: non-static method getFloat() cannot be referenced from a static context

basicHourlyWage = InputReader.getFloat();

^

D:\UNI\JAVA\WageCalculator.java:54: non-static method getString() cannot be referenced from a static context

familyName = InputReader.getString();

^

D:\UNI\JAVA\WageCalculator.java:56: non-static method getString() cannot be referenced from a static context

firstName = InputReader.getString();

^

D:\UNI\JAVA\WageCalculator.java:58: non-static method getString() cannot be referenced from a static context

skillGrade = InputReader.getString();

^

D:\UNI\JAVA\WageCalculator.java:60: non-static method getInt() cannot be referenced from a static context

numberOfWeeks = InputReader.getInt();

^

D:\UNI\JAVA\WageCalculator.java:65: non-static method getInt() cannot be referenced from a static context

hoursPerWeek = InputReader.getInt();

^

Can someone tell me why am I getting this errors.

Best regards

Luis

[2791 byte] By [Luis_Moreiraa] at [2007-11-26 14:58:06]
# 1
When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to read.
cotton.ma at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 2
thank you cotton,I sort of did but is not quite right, I will try to do better next time.best regardsLuis
Luis_Moreiraa at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 3

InputReader is a class, not an instance. You need an instance of InputReader, in order to use its instance-methods - that's what the errors are telling you.

If you do something likeBufferedReader in

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

and then use "in." everywhere you currently have "InputReader.", it may help.

Grant

(Note: the example is straight from the Javadoc for InputStreamReader)

ggaineya at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 4

> When you post code, please use[code] and

> [/code] tags as described in

> [url=http://forum.java.sun.com/help.jspa?sec=formattin

> g ]Formatting tips[/url] on the message entry

> page. It makes it much easier to read.

Well, I'll give the OP points for at least trying. And he gave us the code, and the actual errors, and a real question that was answerable given the info he'd given us.

I will confess that I was not quite sure what to do with the posting for a bit - one so rarely sees a posting that can be reasonably addressed here anymore...

G

ggaineya at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 5

> > When you post code, please use[code] and

> > [/code] tags as described in

> >

> [url=http://forum.java.sun.com/help.jspa?sec=formattin

>

> > g ]Formatting tips[/url] on the message

> entry

> > page. It makes it much easier to read.

>

> Well, I'll give the OP points for at least

> trying.

Yes but when I see italics I give up because it means something got eaten in the code or messages.

cotton.ma at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 6
Hi G,Thank you very much, as soon as I read your reply I could almost see my mistake in my head. I was calling the class itself, ie InputReader.getInt no the instance of it that I create is the constructor, inputReader.thanks for your help.Best regardsLuis
Luis_Moreiraa at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 7
Hi Guys,Now I have another error, when running the program I get the error: " Exception in thread "main" java.lang.NoClassDefFoundError: WageCalculator".Any ideas?best regardsLuis
Luis_Moreiraa at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 8
did you compile itjavac -cp . WageCalculator.java/Then run itjava -cp . WageCalculatorYou are probably suffering from a classpath issue. The -cp options sets the classpath to the path that follows, in my example a . to represent the current
SomeoneElsea at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 9

Hi Tim,

I agree with you,this is definitely a classpath issue and I do not know how to solve it. I also tried to run it on my normal IDE, BlueJ and it did not run. Only when I created a new project and created the classes again by copying the contents of the original classes, did the program Run.

This is the command line in TextPad:

command: javac.exe

parameters: -cp $FileDir $File

initial folder: $FileDir

When I started the parameter -cp $FileDir was not included so I added that which allowed for the compiling of the program.

Best regards

Luis

Luis_Moreiraa at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...
# 10
Hi Tim,I found the problem, like you sujested I added to the comand for running the program the parameter -cp $FileDir, and that solve the problem.thank you very much.Best regardsLuis
Luis_Moreiraa at 2007-7-8 8:46:54 > top of Java-index,Java Essentials,New To Java...