Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to call int value from another class

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to call int value from another class

    Hello guys,

    So if i have something like

    int b= 0;
     
    if ( b < numOfDay){
     
    exit;
     
    } else{ 
     
    do this;
     
    }
    }
    numOfDay is from another class. How can I access it from this class?

    thanks


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to call int value from another class

    Hey IDK12,

    You can do it like this:

    public class TestClass1 {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
    	public static void main(String[] args){
     
    		TestClass2 tc2 = new TestClass2();
     
    		int b = 0;
     
    		if (b < tc2.numOfDay){
    			System.out.println("b is less than " + tc2.numOfDay);
    		}else{
    			System.out.println("b is more or equal than " + tc2.numOfDay);
    		}
    		}
     
    	}

    public class TestClass2 {
     
    	int numOfDay = 420;
     
    }

    Output:

    b is less than 420
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call int value from another class

    Thanks. Great example. I also have other questions. I amcreating a gpa calculator in a multithread( classes) and my friend told me not to do it that way until i learned all about the java concept. So I am bring them back to one single thread but i having problem. The average always gives me 0. Any ideas why ?

      import java.util.Scanner;
    public class Gpa {
     
      double finalGpa;
     
      double firstGpa;
     
      double gpaAverage;
     
     
     public void setGrade(double grade){
     
      Gpa input = new Gpa();
     
      double gpaGradeOne;
     
      double gpaGradeTwo;
     
      double gpaGradeThree;
     
      double gpaGradeFour;
     
          if(( grade <= 100 && grade > 89)){
     
           input.calculatingGpa(4);   
     
        } else if ( grade <=89 && grade > 79){
     
           input.calculatingGpa(3);
     
        } else if(grade <= 79 && grade > 69){
     
          input.calculatingGpa(2);
     
        } else if( grade <= 69 && grade >= 0) {
     
          input.calculatingGpa(1);
     
        } else{
     
          System.out.println("Invalid");
     
        }
     }
     
      public void calculatingGpa(double singleGpa){
     
     
      firstGpa+= singleGpa;
     
      }
     
      public double get(){
     
        return finalGpa = firstGpa;
     
      }
     
        public static void main(String[] args){
     
          Gpa value = new Gpa();
     
          Gpa calling = new Gpa();
     
        Scanner get = new Scanner(System.in);
     
        int b = 0;
     
        double grade;
     
            System.out.println("How many class you have?");
     
          int numClass = get.nextInt();
     
          while ( b < numClass){
     
            System.out.println("What is the gade?");
     
             grade = get.nextInt();
     
              value.setGrade(grade);
     
              b++;
     
                }
     
           System.out.println("GPAaverage is:" + calling.get());
     
     
    }
     }
    Last edited by IDK12; January 13th, 2011 at 02:23 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to call int value from another class

    I think I might know. You need to call setGrade() somewhere.

    Maybe like this:
    private double grade;
    public GPA(double grade)
    {
    this.grade = grade;
    setGrade(grade);
    }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to call int value from another class

    I think perhaps you're going about this the wrong way.
    Here's what you should do.

    • Have a GPA class. (You should need only 1 class).
    • You can have a GPA in each class.
    • You can use an array of type GPA(an array looks like this className[]. It has length a length, but since it starts at index 0, you want to use a for loop to stop before array.length. ) This array should go inside your main method.
    • Make sure in your for loop to say to ask the user for each class to input the grade (circa 0-100) and then use that take initialize each GPA object in the array. Go from index 0 to index the number of classes the user said they had -1 in the for loop.
    • [*]gpa[index] = new GPA(valuePutInByUser);[*]
    • You should have a GPA constructor with a double value.
    • You should have a char variable called grade declared before the constructor and initialized inside of it.
    • You should call setGrade(double value) inside that constructor. It should set the grade to the corresponding
    • letter grade for each value. (e.g. if (value == 95) grade = 'A'. )
    • You should have a char method called getGrade() .
    • You should have a double method getGPA(char grade2). It should return 4.0 if grade == 'A', 3.0 if B and so on.
    • You should have, inside your main class, a double variable called finalGPA, originally initialized to 0, that will later be set using a for loop. Inside the for loop, you should set finalGPA to finalGPA + the value of that index in the GPA array.
    • You should create and initialize finalGPA before you reach the for loop.
    • You should also have an int variable called numberOfClasses, created and initialized to 0 before reaching the for loop. When the user enters the number of classes, you change the value of numberOfClasses to that number entered.
    • [*]for (int i =0; i < array.length; i++)[*]{[*]finalGPA = finalGPA + array[i];[*]}
      [*]
    • Then you have final GPA divided by total number of classes
    • [*]finalGPA = finalGPA/numberOfClasses;[*]
    • Now you have your final GPA.

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call int value from another class

    Quote Originally Posted by javapenguin View Post
    I think I might know. You need to call setGrade() somewhere.

    Maybe like this:
    private double grade;
    public GPA(double grade)
    {
    this.grade = grade;
    setGrade(grade);
    }
    wait, i am a little confused. about this method can you explain a little more. Why do i need this method? and i put your method in my code. it's still not correct.
    Last edited by IDK12; January 13th, 2011 at 07:37 PM. Reason: additional info

  7. #7
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: How to call int value from another class

    i think the problem lies with calling the object 'calling' instead of 'value' in your last prinln method:

    System.out.println("GPAaverage is: " + value.get);

    the values of grade are stored in the value object and not the calling object.

  8. #8

    Default Re: How to call int value from another class

    We can not use neighbour's things directly. If we want to use then we should be relation or we have to ask neighbour. But you can not use if the thing is privacy for them .

    Likewise in java world, in order to access another class members we have to be relation(means child class) or we have to use object.
    There're many things to say about this, but her i am saying only few things. Let's see one simple example:

    class first
    {
    public int a;
    private int b;
    protected int c;
    //rest code
    }
    class second extens first //child of first
    {
    //rest code
    public void access()
    {
    S.o.p(a);
    }
    class third
    {
    //rest code
    void method1{
    first f=new first();
    f.a=2;
    }
    }

    in the above code b can not accessed outside of the first class. C can be used by first class and their child class.

Similar Threads

  1. [SOLVED] Trying to call web service (and failing)
    By jamesruk21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 11:35 AM
  2. Call with a different signature
    By brajesh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2010, 01:05 PM
  3. call win32 api from java
    By bapu in forum Java Native Interface
    Replies: 3
    Last Post: November 7th, 2010, 08:42 AM
  4. JSP freezes after AJAX call !!
    By java_freek in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 16th, 2010, 10:31 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM