7 Replies - 164 Views - Last Post: 23 February 2012 - 07:10 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

getResult giving the same result regardless of input

Posted 23 February 2012 - 06:29 PM

I am working on a challenge for my programming class and run into a little problem. The challenge is to count the number of digits in a number that is inputted by the user. I had the code working fine (although it didnt work with numbers past a certain number of digits) when it was in the main class, but part of the challenge is that we have to put it into a seperate class and call it from the main class.

The code (mostly) works since i moved it to the seperate class, but the problem is that it will only return an answer of 4 regardless of how many digits are actually in the number. I cant see what the problem is, the code worked fine when it was in the main class so i assume that the issue is somewhere when it is being sent from the seperate class back to the main. Here is the code that i am using:

Digit digit;
       digit = new Digit();
        digit.setNum(txtNum.getText());
        lblResult.setText(digit.getResult());
        System.out.println(digit.getResult());


That section is inside a button click event.

public class Digit {
    
    private String num;
    private String result;
    

    Digit() {
        result = "unassigned";
    }
    
    public String getNum() {
        return num;
    }
    public void setNum(String number) {
        num = number;
    }
    
    public String getResult() {
        String result = "The number of digits is: " + length;
        return result;
    }
    
    
        int length = String.valueOf(num).length();
        
}



Thats the code from the seperate class. Can someone point out why it keeps giving the same result every time?

Is This A Good Question/Topic? 0
  • +

Replies To: getResult giving the same result regardless of input

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:36 PM

This line should be inside of the method getResult:
  int length = String.valueOf(num).length();


But it would be better if you remove that line and just modify:
   String result = "The number of digits is: " + num.length();


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:41 PM

So here is your class

public class Digit {
    
    private String num;        // will be assigned null at instantiation time
    private String result;     // will be assigned null at instantiation time
    
    // constructor that assigns "unassigned" to result
    Digit() {
        result = "unassigned";    // this.result will be "unassigned" and num will be null
    }
    

    public String getNum() {
        return num;             // will return null unless setNum() was called
    }
    public void setNum(String number) {
        num = number;           // set num to number
    }
    
    public String getResult() {
        String result = "The number of digits is: " + length;   // creates a variable result
        return result;
    }
    
        // creates a instanation time at that time num is null
        int length = String.valueOf(num).length();
        
}




So getResult() will always return "The number of digits is: "....
Was This Post Helpful? 0
  • +
  • -

#4 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:43 PM

View Postguido-granobles, on 23 February 2012 - 06:36 PM, said:

This line should be inside of the method getResult:
  int length = String.valueOf(num).length();


But it would be better if you remove that line and just modify:
   String result = "The number of digits is: " + num.length();



Thanks alot, that works now. However is there a certain limit to the number of digits that can count that way? I know integers can only go so large, but these are set as strings.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:45 PM

Surely enough to accomodate any String you can store in memory
Was This Post Helpful? 0
  • +
  • -

#6 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:48 PM

View Postpbl, on 23 February 2012 - 06:45 PM, said:

Surely enough to accomodate any String you can store in memory

Great, guess i must have missed actually clicking the button cause for a moment there i thought it wasnt working cause it didnt tell me the result when i clicked it. But i tried again and it's working. Thanks alot.
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 06:49 PM

It can be as long as Integer.MAX_VALUE. I think something like 2GB.. sure you do not want a string like that.
Was This Post Helpful? 0
  • +
  • -

#8 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: getResult giving the same result regardless of input

Posted 23 February 2012 - 07:10 PM

View Postguido-granobles, on 23 February 2012 - 06:49 PM, said:

It can be as long as Integer.MAX_VALUE. I think something like 2GB.. sure you do not want a string like that.

Doh, after rereading the challenge i guess i have to change everything over to integers...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1