Creating class gives "cannot find symbol" error

L

luch

Help! I'm trying to do a simple program, where the user is prompted to
input the number of dimensions to specify for a box; then, 1 of 3
classes of box() are implemented:

0 dimensions // then box box1 = new box();
1 dimension // then box box1 = new box(len);
3 dimensions // then box box1 = new box(len, wid, ht)

but when I compile, I get the message:

Z:\Chapter 08\box.java:107: cannot find symbol
symbol : variable box1
location: class box
System.out.println(box1.toString());

The class assignments are done within an if statement (the commented
switch structure also gave me an error, something about how box1 is
already assigned, when it gets to the second case.)

My code is below - any help is greatly appreciated.
**********************************************************
import java.util.*;
import java.io.*;

public class box
{
public static double len, wid, ht, larger;
public static double largerlen, largerwid, largerht;
static String outmsg;
static Scanner console = new Scanner(System.in);
static char dimen;



public double boxVol(double len, double wid, double ht)
{
return len * wid * ht;
}

public double boxArea(double len, double wid, double ht)
{
return (2 * len * wid) + (2 * len * ht) + (2 * wid * ht);
}

public void makeLarger(double larger)
{
this.len = this.len * larger;
this.wid = this.wid * larger;
this.ht = this.ht * larger;
outmsg = "After each dimension is multiplied by " +
String.format("%.2f",larger)
+ ", the box measurements are:\n";
}

public void makeLarger(double largerlen, double largerwid, double
largerht)
{
this.len = this.len + largerlen;
this.wid = this.wid + largerwid;
this.ht = this.ht + largerht;
outmsg = "After length is increased by " +
String.format("%.2f",largerlen)
+ ",\n width is increased by " +
String.format("%.2f",largerwid)
+ ",\n and height is increased by " +
String.format("%.2f",largerht)
+ ",\n the box measurements are: \n";
}


public String toString()
{
String str = "";
str = "Box dimensions:\n"
+ "Length = " + String.format("%.2f",len)
+ " Width: = " + String.format("%.2f",wid)
+ " Height = " + String.format("%.2f",ht) + "\n";
str = str + "Area = " + String.format("%.2f",boxArea(len, wid, ht))
+ " Volume = " + String.format("%.2f",boxVol(len, wid, ht));
return str;
}


public static void main (String[] args) throws FileNotFoundException
{
System.out.println("This program will calculate the dimensions of a
box based on the\n"
+ "values you enter.");
System.out.println("Enter the number of dimensions you want to
specify:\n:"
+ "0 for no dimensions specified (default of 1) or\n"
+ "1 for the same dimension to be applied to all 3 (len, wid, ht)
or\n"
+ "3 to specify each individual dimension\n");

dimen = console.next().charAt(0);

if (dimen == '0')
// switch (dimen)
// {
// case '0':
{
box box1 = new box();
}
// break;
else if (dimen == '1')
{
// case '1':
System.out.println("Enter the length of the box:");
len = console.nextDouble();
box box1 = new box(len);
// break;
}
else

// case '3':
{
System.out.println("Enter the length of the box:");
len = console.nextDouble();

System.out.println("Enter the width of the box:");
wid = console.nextDouble();

System.out.println("Enter the height of the box:");
ht = console.nextDouble();

box box1 = new box(len,wid,ht);
}
// break;
// }

System.out.println(box1.toString());
System.out.println("-----------------------------");
System.out.println();

System.out.println("Enter one value to multiply each dimension by:");
larger = console.nextDouble();

box1.makeLarger(larger);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();

System.out.println("=============================");

System.out.println("Enter a value to add to the length:");
largerlen = console.nextDouble();

System.out.println("Enter a value to add to the width:");
largerwid = console.nextDouble();

System.out.println("Enter a value to add to the height:");
largerht = console.nextDouble();


box1.makeLarger(largerlen,largerwid,largerht);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();

System.out.println("=============================");



}
public box()
{
this.len = 1.0;
this.wid = 1.0;
this.ht = 1.0;
}

public box(double len)
{
this.len = len;
this.wid = len;
this.ht = len;
}

public box (double len, double wid, double ht)
{
this.len = len;
this.wid = wid;
this.ht = ht;
}
}
 
S

sweet_thiruvonam

Hello,
Happy Harmony is the fastest growing matrimonial portal for
Indians.
You can email and IM other members without paying anything on this
site.
The amazing thing is that this site is totally free. Absolutely free.
Cannot believe? Then click on this link to visit and register Happy
Harmony.
http://www.happyharmony.com/?idAff=14
Background check is the new facility they have added now. You can do a
free
background check including age, address, phone numbers, property
owneship
information etc of anybody in the US.

Regards,
Resh
 
A

Andrew Thompson

Hello,
Happy Harmony is the fastest growing matrimonial portal for
Indians.

Well that's truly wonderful, ..and it would relate to
luch's '"cannot find symbol" error' how?

Three suggestions
1) Stop spamming
2) Post your posts under a relevant title, rather than
under existing threads
3) Stop spamming
 
H

HalcyonWild

luch said:
if (dimen == '0')
{
box box1 = new box();
}
else if (dimen == '1')
{
System.out.println("Enter the length of the box:");
len = console.nextDouble();
box box1 = new box(len);
}
else

{
System.out.println("Enter the length of the box:");
len = console.nextDouble();

System.out.println("Enter the width of the box:");
wid = console.nextDouble();

System.out.println("Enter the height of the box:");
ht = console.nextDouble();

box box1 = new box(len,wid,ht);
}
System.out.println(box1.toString());

You are saying
box box1 = new box(whatever), inside the if and else blocks. Remember
scope. box1 will not be available outside the block.

try something like below
box box1 ;

if (dimensions = whatever)
{
box1 = new box( args );
}

box1 is now available outside the if else blocks.
 
B

Bjorn Abelli

Help! I'm trying to do a simple program, where the user is prompted to
input the number of dimensions to specify for a box; then, 1 of 3
classes of box() are implemented:

0 dimensions // then box box1 = new box();
1 dimension // then box box1 = new box(len);
3 dimensions // then box box1 = new box(len, wid, ht)

but when I compile, I get the message:

Z:\Chapter 08\box.java:107: cannot find symbol
symbol : variable box1
location: class box
System.out.println(box1.toString());


It's a question of "scope".

You declare the variable "box1" within an if-statement. When the
if-statement is done, so is the variable...
if (dimen == '0')
{
box box1 = new box();
}
else if (dimen == '1')
{
box box1 = new box(len);
}

etc...

You can easily resolve this by declaring it before the if-statement, end
assign the right value to it within the if-statement, like this:

box box1 = null;

if (dimen == '0')
{
box1 = new box();
}
else if (dimen == '1')
{
box1 = new box(len);
}

etc...

Although it's common practice in Java to use Capitalized names for classes
(ie Box instead of box).


// Bjorn A
 
L

luch

Bjorn, HalcyonWild,

Thanks for the tip. I figured it was something like that, just didn't
know enough to know what. Much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top