Hi All

package apple;

import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Mates
 */
public class Apple {
   private double sample(){
       double total;
         int a=120;
         Scanner as=new Scanner(System.in);
         System.out.println("enter the value");
         double i=as.nextDouble();
         total=i*a;
         System.out.println("apple"+i+"kg"+" "+total);
         return total;
       
   }
   public double sample1(){
       int b=50;
       double total1;
       System.out.println("enter the value of ");
       Scanner bs=new Scanner(System.in);
       double j=bs.nextDouble();
       total1=j*b;
       System.out.println(+total1);
       return total1;
       }
   public void display(double total,double total1){
       double tot;
       tot=total+total1;
       System.out.println("total is"+tot);
   }
   
    
    public static void main(String[] args) throws IOException {
        
         
               Apple sam=new Apple();
               sam.sample();
               sam.sample1();
               [B]sam.display(total, total1);[/B]
         }
}

Here i am using sample and sample1 these two methods returns total and total1.While i run this piece of code throws an error.How can i get these two return values(total and total1) in to the main method and it is call to the display method to get a resultant output.Please let me know what kind of error it is...:-/


Thanks:)

Recommended Answers

All 5 Replies

what error is thrown?

total and total1 are local variables in methods sample and sample1 respectively. They do not exist outside those methods, and cannot be accessed from outside those methods.

public static void main(String[] args) throws IOException {
        
         
               Apple sam=new Apple();
               double a,b;
               a=sam.sample();
               b= sam.sample1();
               sam.display(a, b);
         }

its works

well, either that, or

public static void main(String args[]){
Apple sam = new Apple();
sam.display(sam.sample(), sam.sample1());
}

Thanks to all,i resolved this problem

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Mates
 */
public class Test {
    double total=0.0;
    double total1=0.0;
   private double sample(){
       
         int a=120;
         Scanner as=new Scanner(System.in);
         System.out.println("enter the value");
         double i=as.nextDouble();
       
         total=i*a;
         System.out.println("apple"+i+"kg"+" "+total);
         return total;
       
   }
   public double sample1(){
       int b=50;
       
       System.out.println("enter the value of ");
      Scanner bs=new Scanner(System.in);
       double j=bs.nextDouble(); 
        
       total1=j*b;
       System.out.println(+total1);
       return total1;
       }
   public void display(){
       double tot;
       tot=total+total1;
       System.out.println("total is"+tot);
   }
   
    
    public static void main(String[] args) throws IOException {
        
         
               Test sam=new Test();
               sam.sample();
               sam.sample1();
               sam.display();
                       
         }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.