Creating class gives "cannot find symbol" error

Discussion in 'Java' started by luch, Oct 5, 2005.

  1. luch

    luch Guest

    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;
    }
    }
    luch, Oct 5, 2005
    #1
    1. Advertising

  2. luch

    Guest

    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
    , Oct 5, 2005
    #2
    1. Advertising

  3. wrote:

    > 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
    Andrew Thompson, Oct 5, 2005
    #3
  4. luch

    HalcyonWild Guest

    luch wrote:
    > 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.
    HalcyonWild, Oct 5, 2005
    #4
  5. luch

    Bjorn Abelli Guest

    "luch" wrote...
    > 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
    Bjorn Abelli, Oct 5, 2005
    #5
  6. luch

    luch Guest

    Bjorn, HalcyonWild,

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


    > 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
    luch, Oct 5, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny
    Replies:
    1
    Views:
    4,665
    Andrew Thompson
    May 26, 2004
  2. Replies:
    5
    Views:
    6,526
    Tilman Bohn
    Dec 16, 2004
  3. Taria
    Replies:
    14
    Views:
    1,657
    Taria
    Oct 2, 2007
  4. powah
    Replies:
    5
    Views:
    2,081
    Arne Vajhøj
    Mar 22, 2009
  5. Replies:
    6
    Views:
    1,728
Loading...

Share This Page