Closed Thread
Results 1 to 5 of 5

Thread: Retrieving Variable data

  1. #1
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Retrieving Variable data

    I was wondering how I would go about retrieving a value assigned to int (or any primitive data type) in one ... object I think (I wouldn't mind some clairifacation on that either) to another.

    I'm writing a program to calculate error for my physics labs.

    Here if is :

    /*
    * author : Dawson Reid
    * date : Feb 9th, 2010
    * problem : To clalculate error with in labs
    */

    package ErrorCalculator;

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    //Tittle
    System.out.println("Welcome to the Error Calculator");

    //Input type of error calculation required
    System.out.println("Would you like to calculate : ");
    System.out.println("1) avg error");
    System.out.println("2) + or - error");
    System.out.println("3) * or / error");
    System.out.println("\nInput a number : ");

    //Choice
    int a = sc.nextInt();

    }

    /*
    * Calculations to fallow
    */

    public static void calc(String[] args) {

    if (a == 1) {

    }

    }

    }

    In the "public static void calc" how would I make it recognize the value put into the "public static void main" for the value of a?

    p.s - How do I place my code into the scroll box thingy others use?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Retrieving Variable data

    well, about the a's value, just make calc to get integer parameter and call calc with a.
    for the second question, select your code and click on the '#' button in the message box

  4. #3
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: Retrieving Variable data

    Do you think you or someone else would be able to put up a short example of what you mean please.

  5. #4
    QuackWare is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    95
    Rep Power
    8

    Re: Retrieving Variable data

    You have to call the method "calc" and provide it with the a variable as a parameter to the method. I have bolded the portions of your code that I changed to make it clear for you.

    Code:
    * author : Dawson Reid
    * date : Feb 9th, 2010
    * problem : To calculate error with in labs
    */
    
    package ErrorCalculator;
    
    import java.util.Scanner;
    
    public class Main {
    
    public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    
    //Tittle
    System.out.println("Welcome to the Error Calculator");
    
    //Input type of error calculation required
    System.out.println("Would you like to calculate : ");
    System.out.println("1) avg error");
    System.out.println("2) + or - error");
    System.out.println("3) * or / error");
    System.out.println("\nInput a number : ");
    
    //Choice
    int a = sc.nextInt();
    
    
    //You call the method "calc" here
    calc(a);
    
    }
    
    /*
    * Calculations to fallow
    */
    //the (int a) portion of the method tells your program that this method always expects an int when called.
    //You provide it with the int a you received from the user.
    public static void calc(int a) {
    
    //You can now use "a" in your code below.
    if (a == 1) {
    
    }
    
    }
    
    }

  6. #5
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: Retrieving Variable data

    Cool, thank you. Really helped.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to get variable with data using jquery
    By lol33d in forum JavaScript and CSS
    Replies: 3
    Last Post: 11-04-2011, 05:39 PM
  2. Quick question about retrieving XML data from oEmbed
    By Codeturk in forum PHP Development
    Replies: 3
    Last Post: 03-02-2011, 06:09 PM
  3. Replies: 2
    Last Post: 01-06-2011, 02:58 AM
  4. Problem in Another way of retrieving data
    By newphpcoder in forum PHP Development
    Replies: 1
    Last Post: 01-05-2011, 05:08 PM
  5. Legal Concerns with retrieving certain data
    By Calgon in forum Business and Legal
    Replies: 4
    Last Post: 11-27-2010, 05:49 AM

Tags for this Thread

Bookmarks

Posting Permissions

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