Results 1 to 11 of 11
  1. #1
    lekimo is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Trying to learn methods.

    I'm trying to make a simple program so that in the main function the user should only have to write value.addera(); or value.subtrahera(); and the program will either +1 or -1 on the starting value. i'm running into a strange error that says

    Java Code:
    calc@5acac877
    here is my code:

    Java Code:
    public class Räknare {
    	
    	public static void main(String[] arg)
    	{
    		calc value = new calc();
    		value.dec(7, 5 , 10); // start, min, max
    		value.addera();
    		String show = value.toString();
    		System.out.println(show);
    	}
    	
    }
    and

    Java Code:
    public class calc
    {
    	int minvärde,maxvärde,startvärde;
    	public void dec(int start,int min, int max) 
    	{
    		if(start < min || start > max)
    		{
    			System.out.println("Ditt värde gick över gränsen");
    		}
    		
    		if(min == 0 && max == 0)
    		{
    			min = Integer.MIN_VALUE;
    			max = Integer.MAX_VALUE;
    		}
    		minvärde = min;
    		maxvärde = max;
    		startvärde = start;
    	}
    	
    	public void addera()
    	{
    	startvärde += 1;
    	}
    	public void subtrahera()
    	{
    	startvärde -= 1;
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Trying to learn methods.

    That's not an error. That's a String representation of an Object that has not overridden the toString() method. Think about it this way: how does Java know how to display an Object as a String? It doesn't, unless you tell it how to by overriding the toString() method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Trying to learn methods.

    int minvärde,maxvärde,startvärde;

    And never ever use Umlauts in java code!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Trying to learn methods.

    Quote Originally Posted by PhHein View Post
    int minvärde,maxvärde,startvärde;

    And never ever use Umlauts in java code!
    Why not?
    It's perfectly legal, and seems unecessarily Anglo-centric to bar them?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Trying to learn methods.

    I've have heard reports about strange effects. It's generally advised here in Germany not to do it. Something about different default system encodings on servers and developer systems, if I recall corretly.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Trying to learn methods.

    System encodings should have no effect on variables in Java code, as it should all be UTF.
    I can see something when dealing with Strings that contain non-ASCII, but that's character encoding and nothing to do with Java and the compiler.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trying to learn methods.

    I prefer a random combination of Cyrilic, Chinese and Arabic characters (and umlauts) just to poke fun at those ASCII-centric pommies and for obfuscation reasons ...

    kind regards,

    乔斯
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Trying to learn methods.

    I would laugh if it turns out that means something rude...

    Sadly you managed to spell Jos, unless Google translate is pulling a fast one.
    I live in hope.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trying to learn methods.

    Quote Originally Posted by Tolls View Post
    I would laugh if it turns out that means something rude...

    Sadly you managed to spell Jos, unless Google translate is pulling a fast one.
    I live in hope.
    You know me, I am not like that; or as we always use to say: 哦,你必须是一个愚蠢的英语猪狗,我扭动我的生殖器您对我们厂总的方向

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    lekimo is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Trying to learn methods.

    Quote Originally Posted by KevinWorkman View Post
    That's not an error. That's a String representation of an Object that has not overridden the toString() method. Think about it this way: how does Java know how to display an Object as a String? It doesn't, unless you tell it how to by overriding the toString() method.
    I see, the way i did it was this:

    Java Code:
    	@Override
    	public String toString()
    	{
    		String resultat = Integer.toString(startvärde);
    		return resultat;
    	}
    I suppose this is the only solution? Since as you said java does not know how to display an object as a string.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Trying to learn methods.

    Quote Originally Posted by lekimo View Post
    I see, the way i did it was this:

    Java Code:
    	@Override
    	public String toString()
    	{
    		String resultat = Integer.toString(startvärde);
    		return resultat;
    	}
    I suppose this is the only solution? Since as you said java does not know how to display an object as a string.
    Did that work?

    I wouldn't say it's the only solution. You could have written another method and called that, or created a method that takes a calc (which should be Calc, by the way) Object and returns a String, or... the options are endless. But since you were already calling the toString() method, it does make sense to specify what that method should do.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  4. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  5. Replies: 1
    Last Post: 10-05-2011, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •