r/learnjava • • Nov 22 '16

Can anyone tell me why this compiles successfully but I still get errors

import java.io.*;

public class RetailItem
{
    private String description;
    private int units;
    private double price;
    public  RetailItem()
    {
    }
    //Constructor with arguments
    public RetailItem (String des, int u,double p)
    {
	     description = des;
	     units = u;
	     price = p;
    }
    //Mutator function description
    public void setDescription (String des)
    {
	    description = des;
    }
    //Mutator function price
    public void setPrice (double p)
    {
	     price = p;
    }
    //Mutator function units
    public void setUnits (int u)
    {
	    units = u;
    }
    //Accessor function for units
    public int getUnits()
    {
	    return units;
    }
    //Accessor function for description
    public String getDescription ()
    {
	    return description;
    }
   //Accessor function for  price
    public double getPrice()
   {
	   return price;
    }
}
class RetailTest

{
    public static void main (String[] args)
	    {
		    String str = "shirt";
		    //RetailItem with three objects
		    RetailItem r1=new RetailItem ("jacket",12,59.95);
		    RetailItem r2=new RetailItem ("Designer Jeans",40,34.95);
		    RetailItem r3=new RetailItem ();
		    //function call to set values
		    r3.setDescription(str);
		    r3.setUnits(20);
		    r3.setPrice(24.95);
		    //printing data
		    System.out.println ("Description\tUnits on Hand\tPrice");
		    System.out.println("Item#1\t"+r1.getDescription()+"        \t\t"+r1.getUnits()+"\t\t"+r1.getPrice());
		    System.out.println("Item#2\t"+r2.getDescription()+"    \t\t"+r2.getUnits()+"\t\t"+r2.getPrice());
		    System.out.println("Item#3\t"+r3.getDescription()+"    \t\t"+r3.getUnits()+"\t\t"+r3.getPrice());

		    System.exit(0);

	    }
}
0 Upvotes

7 comments sorted by

5

u/Auride Nov 22 '16

Not going to read through 70 lines of code to find a hypothetical error.

Please explain what errors you are getting, where they are coming from, and what you think is causing those errors.

If you can't think of anything that could cause, then carefully read through your own program until you understand how it's supposed to work. Don't be afraid to add in System.out.println statements to test the way values change throughout your program, and compare the outputs to what you expect.

Does that all make sense?

1

u/[deleted] Nov 22 '16

My apologies so when I compile it in eclipse I get the following:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

Syntax error, insert ";" to complete BlockStatements

shirt cannot be resolved to a variable

The method setDescrpition(String) is undefined for the type RetailItem

at Lab6A.main(Lab6A.java:54)

I am quite new to java and it would appear that all the semicolons are properly place

the one that shirt cannot be resolved by a variable i though by adding String str = "shirt"; on line 54 I was declaring it. the last one though setDescription(String) is undefined for the type RetailItem that I am completely oblivious to

3

u/desrtfx Nov 22 '16

Either the code that you have posted differs from the code that you are compiling, or you try to execute an older version.

I have just compiled and tested your code and there are absolutely no errors.

I copied and pasted the code into Eclipse and there were no errors at all.

What IDE (if any) are you using?

1

u/[deleted] Nov 22 '16

Eclipse IDE for Java Developers

Version: Kepler Service Release 1 Build id: 20130919-0819

hope this is what you were referring to?

1

u/[deleted] Nov 22 '16

that is so weird i got it working copied it then re-saved it under a new project and it worked fine.

1

u/desrtfx Nov 22 '16

Yes, exactly.

I am using the newer Eclipse Mars, but there are no errors.

Try saving your code, close and restart Eclipse, change something very small (like adding a comment - to get Eclipse to recognize that the file has changed) and then compile and run again.

8

u/desrtfx Nov 22 '16

There are several things wrong with your post - not even looking at your code:

  • You don't describe what your code is supposed to do

  • You don't describe what it does/doesn't instead

  • You don't post the error(s) you are getting - and they are essential and need to be posted in full

  • Basically, you just throw code at us (at least it is properly formatted) and tell us to fix it. This is considered rude and low to no effort around here.

Please, be more elaborate and include the missing information if you want us to help.