Hack Forums
[Java] Error missing return statement - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Error missing return statement (/showthread.php?tid=5949343)



Error missing return statement - blackcoder00 - Mar 10, 2019

hi guys i run my code and i get an error missing return statement i can't define where i'm wrong

Quote:An error occurred:
Test 1
missing return statement
missing return statement
missing return statement
missing return statement


PHP Code

<?php 
import java
.util.Scanner;
import java.math.*;
class 
MyCalculator{
private static final 
Scanner askScanner = new Scanner(System.in);
public static 
double answer;
public static 
double firstNumber;
public static 
double secondNumber; 

public static 
void main(String[] args) {
    
calculator();
}

    public static 
void calculator() {



    
System.out.println("Basic calculator");
    
System.out.println("Pick one:");
    
System.out.println("1. Addition");
    
System.out.println("2. Subtraction");
    
System.out.println("3. Multiplication");
    
System.out.println("4. Division");
    
int pick = askScanner.nextInt();

    if(
pick == 1) {
        
addition(firstNumber,secondNumber);
    }
    else if(
pick == 2) {
        
subtract(firstNumber,secondNumber);
    }
    else if(
pick == 3) {
        
multiplication(firstNumber,secondNumber);
    }
    else if(
pick == 4) {
        
division(firstNumber,secondNumber);
    }
    else {
        
System.out.println("You need to choose 1, 2, 3, or 4");
        
calculator();
    }
    
askScanner.close();
}

    private static 
double getNumbers() {
        
System.out.println("First number?");
        
firstNumber = askScanner.nextDouble();
        
System.out.println("Second Number?");
        
secondNumber = askScanner.nextDouble();
        return 
firstNumber;
    }

    public static 
double subtract(double firstNumber,double secondNumber) {
        
System.out.println("Subtraction");
        
getNumbers();
        
answer = firstNumber - secondNumber;
        
System.out.println("This is the difference of the two numbers:  " + answer);
        
calculator();
        
    }

    public static 
int addition(double firstNumber,double secondNumber) {
       
System.out.println("Addition");
       
getNumbers();
       
answer = firstNumber + secondNumber;
       
System.out.println("This is the sum of the two numbers:  " + answer);
       
calculator();
    }

    public static 
int multiplication(double firstNumber,double secondNumber) {
        
System.out.println("Multiplication");
        
getNumbers();
        
answer = firstNumber * secondNumber;
        
System.out.println("This is the product of the two numbers  " + answer);
        
calculator();
    }

    public static 
int division(double firstNumber,double secondNumber) {
        
System.out.println("Division");
        
getNumbers();
        
answer = firstNumber / secondNumber;
        
System.out.println("This is the quotient of the two numbers:    " + answer);
        
calculator();
    }

} 



RE: Error missing return statement - doot - Mar 10, 2019

the methods subtract, addition, multiplication, and division are defined to return numbers but you don't return anything at the end of them


RE: Error missing return statement - blackcoder00 - Mar 10, 2019

(Mar 10, 2019 07:29 PM)doot Wrote:  the methods subtract, addition, multiplication, and division are defined to return numbers but you don't return anything at the end of them

then i should return answer; at the end ?


(Mar 10, 2019 07:31 PM)blackcoder00 Wrote:  then i should return answer; at the end ?
i get an error :
Quote:ncompatible types: possible lossy conversion from double to int


PHP Code

<?php 
public static int addition(double firstNumber,double secondNumber) {
       
System.out.println("Addition");
       
getNumbers();
       
answer = firstNumber + secondNumber;
       
System.out.println("This is the sum of the two numbers:  " + answer);
       
calculator();
        return 
answer;
    } 



RE: Error missing return statement - Beathoven - Mar 10, 2019

The error that you're currently being given is due to how you have set the return type of your function to be an integer despite your parameters being doubles and therefore your return type a double (as answer is a double and you're adding two doubles which has a type of double). This causes a conversion error. Since you're converting from a double to an integer you would be losing precision which is why it is complaining about "lossy conversion". The easiest way to solve this problem is by changing your function signature to either use integer parameters or return a double.

Integer Parameters:
Code
public static int addition(int firstNumber, int secondNumber) {...}
Returning a Double:
Code
public static double addition(double firstNumber, double secondNumber

My personal opinion is that since all the variables in your class are doubles you should be returning in doubles, otherwise you'll continuously be running into problems like this and losing precision that may be very important, especially if you're making a calculator.

Hope I helped! 😁

Edit: I forgot to mention that the functions subtract, addition, multiplication, and division all have this problem


RE: Error missing return statement - Abruptum - Mar 11, 2019

(Mar 10, 2019 06:45 PM)blackcoder00 Wrote:  hi guys i run my code and i get an error missing return statement i can't define where i'm wrong


PHP Code

<?php 
import java
.util.Scanner;
import java.math.*;
class 
MyCalculator{
private static final 
Scanner askScanner = new Scanner(System.in);
public static 
double answer;
public static 
double firstNumber;
public static 
double secondNumber; 

public static 
void main(String[] args) {
    
calculator();
}

    public static 
void calculator() {



    
System.out.println("Basic calculator");
    
System.out.println("Pick one:");
    
System.out.println("1. Addition");
    
System.out.println("2. Subtraction");
    
System.out.println("3. Multiplication");
    
System.out.println("4. Division");
    
int pick = askScanner.nextInt();

    if(
pick == 1) {
        
addition(firstNumber,secondNumber);
    }
    else if(
pick == 2) {
        
subtract(firstNumber,secondNumber);
    }
    else if(
pick == 3) {
        
multiplication(firstNumber,secondNumber);
    }
    else if(
pick == 4) {
        
division(firstNumber,secondNumber);
    }
    else {
        
System.out.println("You need to choose 1, 2, 3, or 4");
        
calculator();
    }
    
askScanner.close();
}

    private static 
double getNumbers() {
        
System.out.println("First number?");
        
firstNumber = askScanner.nextDouble();
        
System.out.println("Second Number?");
        
secondNumber = askScanner.nextDouble();
        return 
firstNumber;
    }

    public static 
double subtract(double firstNumber,double secondNumber) {
        
System.out.println("Subtraction");
        
getNumbers();
        
answer = firstNumber - secondNumber;
        
System.out.println("This is the difference of the two numbers:  " + answer);
        
calculator();
        
    }

    public static 
int addition(double firstNumber,double secondNumber) {
       
System.out.println("Addition");
       
getNumbers();
       
answer = firstNumber + secondNumber;
       
System.out.println("This is the sum of the two numbers:  " + answer);
       
calculator();
    }

    public static 
int multiplication(double firstNumber,double secondNumber) {
        
System.out.println("Multiplication");
        
getNumbers();
        
answer = firstNumber * secondNumber;
        
System.out.println("This is the product of the two numbers  " + answer);
        
calculator();
    }

    public static 
int division(double firstNumber,double secondNumber) {
        
System.out.println("Division");
        
getNumbers();
        
answer = firstNumber / secondNumber;
        
System.out.println("This is the quotient of the two numbers:    " + answer);
        
calculator();
    }

} 

This would be a better approche:

Code
import java.util.Scanner;
import java.math.*;
public class MyCalculator {
    private static Scanner askScanner;
    public static void main(String[] args) {
        askScanner = new Scanner(System.in);
        while(calculator());
    }

    private static void printMenu(){
        System.out.println("Basic calculator");
        System.out.println("Pick one:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
    }

    private static boolean calculator() {
        printMenu();
        int ask = askScanner.nextInt();
        if(ask < 1 || ask > 4){
            System.out.println("You need to choose 1, 2, 3, or 4");
            return true;
        }
        double firstNumber = getNumbers("First number?");
        double secondNumber = getNumbers("Second number?");
        double result = 0;
        switch(ask){
            case 1:
                result = addition(firstNumber, secondNumber);
            break;
            case 2:
                result = subtract(firstNumber, secondNumber);
            break;
            case 3:
                result = multiplication(firstNumber, secondNumber);
            break;
            case 4:
                result = division(firstNumber, secondNumber);
            break;
        }
        // do something with your result ;D
        askScanner.close();
        return false;
    }

    private static double getNumbers(String askString) {
        System.out.println(askString);
        return askScanner.nextDouble();
    }

    private static double subtract(double firstNumber, double secondNumber) {
        double result = firstNumber - secondNumber;
        System.out.println("Subtraction\n\rThis is the difference of the two numbers:  " + result);
        return result;
    }

    private static double addition(double firstNumber, double secondNumber) {
        double result = firstNumber + secondNumber;
        System.out.println("Addition\n\rThis is the sum of the two numbers:  " + result);
        return result;
    }

    private static double multiplication(double firstNumber, double secondNumber) {
        double result = firstNumber * secondNumber;
        System.out.println("Multiplication\n\rThis is the product of the two numbers  " + result);
        return result;
    }

    private static double division(double firstNumber, double secondNumber) {
        double result = firstNumber / secondNumber;
        System.out.println("Division\n\rThis is the quotient of the two numbers:    " + result);
        return result;
    }

}



RE: Error missing return statement - blackcoder00 - Mar 11, 2019

(Mar 10, 2019 08:19 PM)Beathoven Wrote:  The error that you're currently being given is due to how you have set the return type of your function to be an integer despite your parameters being doubles and therefore your return type a double (as answer is a double and you're adding two doubles which has a type of double). This causes a conversion error. Since you're converting from a double to an integer you would be losing precision which is why it is complaining about "lossy conversion". The easiest way to solve this problem is by changing your function signature to either use integer parameters or return a double.

Integer Parameters:
Code
public static int addition(int firstNumber, int secondNumber) {...}
Returning a Double:
Code
public static double addition(double firstNumber, double secondNumber

My personal opinion is that since all the variables in your class are doubles you should be returning in doubles, otherwise you'll continuously be running into problems like this and losing precision that may be very important, especially if you're making a calculator.

Hope I helped! 😁

Edit: I forgot to mention that the functions subtract, addition, multiplication, and division all have this problem
first welcome to HK and i give u 10 Bytes cause we need more & more developer in HK enjoy and thank you


(Mar 11, 2019 07:18 AM)Abruptum Wrote:  This would be a better approche:

Code
import java.util.Scanner;
import java.math.*;
public class MyCalculator {
   private static Scanner askScanner;
   public static void main(String[] args) {
       askScanner = new Scanner(System.in);
       while(calculator());
   }

   private static void printMenu(){
       System.out.println("Basic calculator");
       System.out.println("Pick one:");
       System.out.println("1. Addition");
       System.out.println("2. Subtraction");
       System.out.println("3. Multiplication");
       System.out.println("4. Division");
   }

   private static boolean calculator() {
       printMenu();
       int ask = askScanner.nextInt();
       if(ask < 1 || ask > 4){
           System.out.println("You need to choose 1, 2, 3, or 4");
           return true;
       }
       double firstNumber = getNumbers("First number?");
       double secondNumber = getNumbers("Second number?");
       double result = 0;
       switch(ask){
           case 1:
               result = addition(firstNumber, secondNumber);
           break;
           case 2:
               result = subtract(firstNumber, secondNumber);
           break;
           case 3:
               result = multiplication(firstNumber, secondNumber);
           break;
           case 4:
               result = division(firstNumber, secondNumber);
           break;
       }
       // do something with your result ;D
       askScanner.close();
       return false;
   }

   private static double getNumbers(String askString) {
       System.out.println(askString);
       return askScanner.nextDouble();
   }

   private static double subtract(double firstNumber, double secondNumber) {
       double result = firstNumber - secondNumber;
       System.out.println("Subtraction\n\rThis is the difference of the two numbers:  " + result);
       return result;
   }

   private static double addition(double firstNumber, double secondNumber) {
       double result = firstNumber + secondNumber;
       System.out.println("Addition\n\rThis is the sum of the two numbers:  " + result);
       return result;
   }

   private static double multiplication(double firstNumber, double secondNumber) {
       double result = firstNumber * secondNumber;
       System.out.println("Multiplication\n\rThis is the product of the two numbers  " + result);
       return result;
   }

   private static double division(double firstNumber, double secondNumber) {
       double result = firstNumber / secondNumber;
       System.out.println("Division\n\rThis is the quotient of the two numbers:    " + result);
       return result;
   }

}

send u 4Bytes ty