Results 1 to 11 of 11
  1. #1
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default Help with really simple coding

    Hi.
    Have been searching around the internet for hours now and still don´t get how to import data from a method to another one.

    Here´s my code so far:

    import javax.swing.*;
    public class N5 {

    public void ändra(double valuta1) {
    double valuta1;
    valuta1 = 1.05;
    }


    public static void main(String[] arg) {
    double total;
    int tal1;
    String indata = JOptionPane.showInputDialog
    (null, "Hur mycket vill du växla?");
    tal1 = Integer.parseInt(indata);


    // Here i wanna add some code to import the stuff i added in "ändra".



    total = tal1 / valuta1;
    JOptionPane.showMessageDialog
    (null, "Du kan få " +total);




    REALLY thankfull for help :)

  2. #2
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    Variables created in a Class' method's are not "remembered" after the method call (This is why you cannot/ do not need to assign them public, private, protected, etc). Where as Object/Class variables are persistent throughout the life of the Object (or class if the variable is Static). If I understand you correctly, one solution would be to take the variables in your method ändra and assign them to class variables.

    Java Code:
    public class N5 {
    private double ändra_valuta1;
    
    public void ändra(double valuta1) {
    double valuta1;
    valuta1 = 1.05;
    ändra_valuta1 = valuta1;
    } 
    ...
    Or more simply:
    Java Code:
    ...
    public void ändra(double valuta1) {
    ändra_valuta1 = valuta1;
    }

  3. #3
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Hi.
    Thanks for your help, but i still cant get it to work.
    This is the code im using now.
    At least its a breakthrough :).
    I changed the swedish words to english, to make it clearer.

    import javax.swing.*;
    public class N5 {
    private double andra_valuta1;

    public void andra(double valuta1) {
    valuta1 = 1.05;
    andra_valuta1 = valuta1;
    }

    public static void main(String[] arg) {
    double total;
    int tal1;
    String indata = JOptionPane.showInputDialog
    (null, "How much?");
    tal1 = Integer.parseInt(indata);

    total = tal1 / andra_valuta1;
    JOptionPane.showMessageDialog
    (null, "You will get" +total);

    }}


    And i get following:

    "C:\Users\tomten\Desktop\allt\N5.java:22: non-static variable andra_valuta1 cannot be referenced from a static context
    total = tal1 / andra_valuta1;
    ^
    1 error

    Tool completed with exit code 1"


    I tried changing it to static, but then i got infinity as answer on "total".

    This program is driving me nuts....

  4. #4
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    Oh I see, you are calling the variable andra_valuta1 inside of the main method. The only thing special about this is that it is a 'Static' method. This means that the method main exists for all instances/Objects of the class N5.
    Why this matters to you is since the variable 'andra_valuta1' is an Object/Instance variable. So, the static (Class) method main has no knowledge of andra_valuta1 (Object/Instance). That was a rather lengthy explanation to simply say the variable andra_valuta1 needs to be made static.
    As so:

    Java Code:
    private [B]static[/B] double andra_valuta1;
    After running your code, it looks like you will need to call the method 'andra' before you execute 'total = tal1 / andra_valuta1;' or at least set the variable 'andra_valuta1' (By default it initializes/set to 0.0 and probably won't get the solution you were looking for... (any int)/0.0 = infinity). I'm not sure why you're not getting a divide by zero error though.
    Last edited by mikeiz404; 01-24-2009 at 01:33 AM. Reason: clarification

  5. #5
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by mikeiz404 View Post
    Oh I see, you are calling the variable andra_valuta1 inside of the main method. The only thing special about this is that it is a 'Static' method. This means that the method main exists for all instances/Objects of the class N5.
    Why this matters to you is since the variable 'andra_valuta1' is an Object/Instance variable. So, the static (Class) method main has no knowledge of andra_valuta1 (Object/Instance). That was a rather lengthy explanation to simply say the variable andra_valuta1 needs to be made static.
    As so:

    Java Code:
    private [B]static[/B] double andra_valuta1;
    After running your code, it looks like you will need to call the method 'andra' before you execute 'total = tal1 / andra_valuta1;' or at least set the variable 'andra_valuta1' (By default it initializes/set to 0.0 and probably won't get the solution you were looking for... (any int)/0.0 = infinity). I'm not sure why you're not getting a divide by zero error though.
    Thanks for answering fast. I have been trying some stuff out, and i noticed that the first method:


    public void andra(double valuta1) {
    valuta1 = 1.05;
    JOptionPane.showMessageDialog(null, "Hello");
    ändra_valuta1 = valuta1;
    }


    wasnt even "read".

    I ran the program, and didnt see a text with "Hello".
    I also added a simple method after the main, and that didnt get "read" either.

    Don´t know if that helps. Thanks for your time man.
    Last edited by tigertomas; 01-24-2009 at 02:07 AM.

  6. #6
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    You might want to read up on some intro/fundamentals to java. The andra method is not called automatically; only the main method is called because java treats it specially. If you want the andra method to be called then you will need to call it in the main method (based on the code you have now).

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Tiger: Here are two links to the Sun's tutorials:

    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Some good explanations and sample of methods:
    Java Method Arguments

    When you declare a method, you have to specify what arguments/parameters it will be receiving and what that method will be returning.
    For example:
    Java Code:
    public static [B][COLOR="Blue"][COLOR="red"]int[/COLOR][/COLOR][/B] mult ([B][COLOR="blue"]int a, int b[/COLOR][/B])
    means that it will be receiving two int type values and will retuning an int value.

    For this example, you can make your method static (see example above).

    Now with ALL this information, modify your porgram and i'm sure it will work.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Yes i finally figured it out. But ofc thats only thanks to you guys :D.
    I gotta say, i wasted loads of hours to something simple...
    That 2nd link of yours was awesome.

    Thanks again !!

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Any time...

    You're welcome... please post you're final code so others can learn too.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    tigertomas is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Here it is. ;)

    import javax.swing.*;
    public class N5 {

    public static double andra() {
    double valuta1 = 1.05;
    return valuta1;
    }

    public static void main(String[] arg) {
    double total;
    int tal1;
    String indata = JOptionPane.showInputDialog
    (null, "How much?");
    tal1 = Integer.parseInt(indata);

    total = tal1 / andra();
    JOptionPane.showMessageDialog
    (null, "You will get" +total);
    }}

  11. #11
    mikeiz404 is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

Similar Threads

  1. swing coding
    By priya_gurnani166 in forum AWT / Swing
    Replies: 8
    Last Post: 01-30-2009, 08:18 AM
  2. coding help
    By accies76 in forum New To Java
    Replies: 5
    Last Post: 11-12-2008, 09:15 PM
  3. Need Help Coding Project
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 04-28-2008, 12:31 AM
  4. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 08:01 AM
  5. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 06:07 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
  •