简体   繁体   中英

How to call strings from other methods into main method?

Hello I'm new to java and need someone to answer a problem I'm having. I have recently started a project to make a calculator in Java. However i'm having a problem with one prat of my code. Basically i can't call a string off from an method. Ive tried varoius other attemps to fix the problem but to no avail. Here is the code:

package CalculatorCore;

import java.util.Scanner;

public class calculations {

    static void firstNumber() {
        Scanner firstNum = new Scanner(System.in);
        System.out.print("First Number: ");
        String n1 = firstNum.next(); //You can see, i put the string in a method

}

    static void secondNumber() {
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Second Number: ");
        String n2 = secondNum.next(); //Here too


}



    public static void main(String[] args) {
    
    
    
        System.out.println("Please Choose one of the following equasions: +, -, * or /");
        System.out.println("");
        System.out.println("");
        mathEquasions();
    
    
        
    }


    static void mathEquasions() {
        Scanner equasions = new Scanner(System.in);
        System.out.print("Enter input: ");
        String e = equasions.next();
        
        if (e.equals("+")) {
            System.out.println("");
            System.out.println("Please enter the first number that you want to add");
            firstNumber();
            System.out.println("");
            System.out.println("Now add the second number");
            secondNumber();
            var plusAnswer = (n1 + n2);  /*The problem is situated here, i need to call the 
            strings from another class*/
         
            System.out.println("");
            System.out.println("");
            System.out.println("Your answer is...");
            firstNumber();.n1
        
        
        
        }
        
        
        
    }
    

I've already used methods to make the user inputs compact so if theres no other way should i remove the methods?

You need to return the numbers you retrieved in your both methods. Don't forget to parse them as integers, using nextInt :

public class calculations {
    static int firstNumber() {
        Scanner firstNum = new Scanner(System.in);
        System.out.print("First Number: ");
        int n1 = firstNum.nextInt();
        return n1;
    }

    static int secondNumber() {
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Second Number: ");
        int n2 = secondNum.nextInt();
        return n2;
    }
}

Then, when calling firstNumber or secondNumber , create new variables to store their return values:

public class calculations {
    static void mathEquasions() {
        Scanner equasions = new Scanner(System.in);
        System.out.print("Enter input: ");
        String e = equasions.next();
        
        if (e.equals("+")) {
            System.out.println("");
            System.out.println("Please enter the first number that you want to add");

            int n1 = firstNumber();

            System.out.println("");
            System.out.println("Now add the second number");

            int n2 = secondNumber();

            var plusAnswer = (n1 + n2);
         
            System.out.println("");
            System.out.println("");
            System.out.println("Your answer is...");
        }
    }
}

welcome to SO! When trying something for the first time it's a good practice to make it as simple as you can, and from there gradually use more complex techniques.

In this case everything is in one class already, so as a first step you could try to put all your code back into the main method .

public static void main(String[] args) {
    //All your code can come here first in the order they are supposed to to be called.
}

As a second step, when it all works, you can extract the parts where you would duplicate code, into separate methods. Like here instead of having firstNumber() and secondNumber() you could have just one, with something like:

static int getNumber() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Number: ");
    int number = scanner.nextInt();
    return number;
}

and you can call the same method to get both numbers:

public static void main(String[] args) {

    //...

    System.out.println("Please enter the first number that you want to add");
    int n1 = getNumber(); // Using the same method for both
  
    System.out.println("");
    System.out.println("Now add the second number");

    int n2 = getNumber(); // Using the same method for both

    //...
}

Learning by doing and jumping into the thick of it is one of the best ways to learn. There are tons of good quality materials freely available (eg. on yt) and they can really boost your skills. That's also how I started learning, so good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM