Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Error missing return statement

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();
    }

} 
Writeln('Give me a Target >');
[Image: giphy.gif]
the methods subtract, addition, multiplication, and division are defined to return numbers but you don't return anything at the end of them
(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;
    } 
Writeln('Give me a Target >');
[Image: giphy.gif]
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
(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;
    }

}
(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
Writeln('Give me a Target >');
[Image: giphy.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by oh9odSep 16, 2020 04:24 PM 5 183 Sep 16, 2020 04:24 PM
Last Post: omboml
[Java] by KusoApr 30, 2017 07:23 AM 11 405 Apr 30, 2017 07:23 AM
Last Post: Owl
[C] by D_MuLaOct 7, 2016 03:27 PM 10 243 Oct 7, 2016 03:27 PM
Last Post: 0xDEAD10CC