3

I am receiving the error cannot find symbol when the code reaches the recursive call for increment and I have no idea why? Here is the code for increment. Any help will be greatly appreciated.

public void increment() {
    Integer first = 0;
    Character ch = num.charAt(num.length()-1);
    Integer last = Character.digit(ch, 10);

    if (num.length() > 1)
    {
        if (last == 9) {
            last = 0;
            if (num.length() >= 2)
            {
            String temp = new String(num.substring(0, num.length()-2));
            temp.increment();
            }
            else
            {
            last++;
            }
        }
    }
    else
    {
        if (last == 9)
        {
            last = 0;
            first = 1;
        }
        else
        {
        last++;
        }
    }

    String t = new String();
    String x = new String();
    t = last.toString();
    x = first.toString();

    if (first > 0)
    {
    num.concat(x);
    }

    num.concat(t);
}

EDIT: I'm really new to java so the more basic you can make your answers, the better. Ok so the error I am receiving is: BigNatural.java.35: cannot find symbol symbol method increment() location: class java.lang.String temp.increment()

And to clear up any other issues here is the whole code.

public class BigNatural {

private String num; 

public BigNatural(String input) {
    num = input;
}


public BigNatural(BigNatural input) {
    num = input.toString();
}

public BigNatural(Integer input) {
    num = input.toString();
}

public BigNatural() {
    Integer i = 0;
    num = i.toString();
}

public void increment() {
    Integer first = 0;
    Character ch = num.charAt(num.length()-1);
    Integer last = Character.digit(ch, 10);

    if (num.length() > 1)
    {
        if (last == 9) {
            last = 0;
            if (num.length() >= 2)
            {
            String temp = new String(num.substring(0, num.length()-2));
            temp.increment();
            }
            else
            {
            last++;
            }
        }
    }
    else
    {
        if (last == 9)
        {
            last = 0;
            first = 1;
        }
        else
        {
        last++;
        }
    }

    String t = new String();
    String x = new String();
    t = last.toString();
    x = first.toString();

    if (first > 0)
    {
    num.concat(x);
    }

    num.concat(t);
}

public void decrement() {
    Character ch = num.charAt(num.length()-1);
    Integer last = Character.digit(ch, 10);

    if(num.length() > 1)
    {
        if(last == 0)
        {
            String temp = new String(num.substring(0, num.length()-2));
            temp.decrement();
        }
        else
        {
        last--;
        }
    }
    else
    {
        if(last > 0)
        {
            last--;
        }
        else
        {
            last = 0;
        }
    }

    String t = new String();
    t = last.toString();
    num.concat(t);
}


public String toString() {
    return num;
}

} public class BigNatural {

private String num; 

public BigNatural(String input) {
    num = input;
}


public BigNatural(BigNatural input) {
    num = input.toString();
}

public BigNatural(Integer input) {
    num = input.toString();
}

public BigNatural() {
    Integer i = 0;
    num = i.toString();
}

public void increment() {
    Integer first = 0;
    Character ch = num.charAt(num.length()-1);
    Integer last = Character.digit(ch, 10);

    if (num.length() > 1)
    {
        if (last < 9) {
                            last++
            }
            else
            {
            last = 0;
            if (num.length() >= 2)
            {
            String temp = new String(num.substring(0, num.length()-2));
            temp.increment();
            }
        }
                    else {
                            last++;
                    }
    }
    else
    {
        if (last == 9)
        {
            last = 0;
            first = 1;
        }
        else
        {
        last++;
        }
    }

    String t = new String();
    String x = new String();
    t = last.toString();
    x = first.toString();

    if (first > 0)
    {
    num.concat(x);
    }

    num.concat(t);
}

public void decrement() {
    Character ch = num.charAt(num.length()-1);
    Integer last = Character.digit(ch, 10);

    if(num.length() > 1)
    {
        if(last == 0)
        {
            String temp = new String(num.substring(0, num.length()-2));
            temp.decrement();
        }
        else
        {
        last--;
        }
    }
    else
    {
        if(last > 0)
        {
            last--;
        }
        else
        {
            last = 0;
        }
    }

    String t = new String();
    t = last.toString();
    num.concat(t);
}


public String toString() {
    return num;
}

}

Bigby
  • 193
  • 2
  • 4
  • 12

2 Answers2

6

String has no method called increment. And of course it isn't a recursive call because you are inside an object(which object? in your code there isn't a class definition) , meanwhile you are invoking increment upon a String object.

In addition your temp field is never used. If you want to share it between method calls you can try something like this:

public void increment (String temp){}

and then pass it while calling it:

String temp = new String(num.substring(0, num.length()-2));
increment(temp);

Of course your function can't work like that. temp parameter should be managed inside your increment method. Review your logic. It's no more a matter of syntax. If you can't change the method signature then declare temp as a field of your BigNatural class:

public class BigNatural {

private String num; 
private String temp
......

and inside increment method simply do:

temp = new String(num.substring(0, num.length()-2));
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • I'm really new to java, so how would I make that call then? – Bigby Apr 19 '11 at 07:43
  • @user714741 : simply write: increment() . (Or this.increment() for explicitly referring to this object) – Heisenbug Apr 19 '11 at 07:44
  • @Big Brown: if you are new to Java I'll suggest you to try Eclipse. It's a good instruments for both developing and learning because it immediately shows you error in the code, with a suggestion for fixing it. – Heisenbug Apr 19 '11 at 07:52
  • I guess I'm just a little confused because I know the test script call is going to involve something like b1.increment() with b1 being a BigNatural. Also if I just call increment() how would temp be the new string that is being worked on. – Bigby Apr 19 '11 at 08:05
  • @Big Brown: I'm not fully understanding you. Anyway increment is called on the current implicit object(this). If you need to pass it a parameter change the signature like: void implement(String s) – Heisenbug Apr 19 '11 at 08:11
  • Ok so making the change to increment() allows it to compile, but the increment doesnt seem to do anything... – Bigby Apr 19 '11 at 08:34
  • @Big Brown: your application logic is quite complicated. You temp field seems to never been used – Heisenbug Apr 19 '11 at 08:42
  • @Big Brown. refactor increment function to accept a string parameter and then pass it. I'll edit my answer – Heisenbug Apr 19 '11 at 08:46
  • bah, I know that would fix it but the grader is going to test the program with a test scrip that is going to look something like BigNatural b1 = new BigNatural(34) followed by a call b1.increment(). If i change increment to accept parameters it would no longer work with that script right? – Bigby Apr 19 '11 at 08:51
  • the problem that i am currently having is that it wont increment or decrement for even the simple base cases where the last number is not 9. – Bigby Apr 19 '11 at 08:52
  • @Big Brown: just a tip: your initial question has been answered. If you need more help but the question is different accept the answer and post another question explaining in details what are you trying to do a which are your doubts . – Heisenbug Apr 19 '11 at 09:03
  • will do, thanks for putting up with me and thanks for the help. – Bigby Apr 19 '11 at 09:04
0

Some other pointers:

String is an immutable class meaning once it is created it can no longer be changed. So, doing

String t = new String();
t = last.toString();

has now sense since you will create 2 String Objects here (last.toString() will create and return a new String).

Simply do:

String t = last.toString();

or even better:

num.concat(last.toString());

Same goes for temp String, simply do:

String temp = num.substring(0, num.length()-2);

Next, be aware of unintentional autoboxing:

Integer first = 0;
first++;

this will create a new Integer Object every time first++ is executed; Integer (as String) is immutable. When calculating simply use the primitive int instead of Integer.

Finally, be careful that you don't create an infinite loop. If I understand your code num will concatenated to so num.length() > 1 will be true always if it was true the first time.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101