Search code examples
javamethodsjava.util.scannergetter-setter

How to put scanner inside a method and its output on a different method


I want to ask the user through a scanner class but I want this scanner to be in a method named readInput() and the output on a different method named writeOutput() using gett-setter

this is my code below:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LaboratoryExercise2 gro = new LaboratoryExercise2();
        String[] SecondQuestion;
        System.out.println("Select the item your purchasing.");
            String Product1  = sc.nextLine();
        System.out.println("Enter the Quantity and price separated by SPACE.");
            SecondQuestion = sc.nextLine().split(" ");
                int Quantity1 = Integer.parseInt(SecondQuestion[0]);
                double Price1 = Double.parseDouble(SecondQuestion[1]);
                double Amount1 = 0;
                Amount1 = Quantity1 * Price1;
                int Quantity = 0 + Quantity1;
                double Amount = 0 + Amount1;
                double Price = 0 + Price1;

I want the output of this to show on a different method

        gro.setGrocery(Price, Quantity, Amount);
    System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " + 
                                                                            gro.getitemPrice() + " each");

    System.out.println("Amount due is " + gro.getamountDue());

This is my whole code:

import java.util.Scanner; 

public class LaboratoryExercise2 {

private double itemPrice;
private int itemQuantity;
private double amountDue;


public void setGrocery(double newitemPrice, int newitemQuantity, double newamountDue) {

    itemPrice = newitemPrice;
    itemQuantity = newitemQuantity;
    amountDue = newamountDue;
}

public double getitemPrice() {
    return itemPrice;
    
}

public int getitemQuantity() {
    return itemQuantity;
    
}

public double getamountDue() {
    return amountDue;
    
}



public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    
    LaboratoryExercise2 gro = new LaboratoryExercise2();
    String[] SecondQuestion;

    System.out.println("Select the item your purchasing.");
        String Product1  = sc.nextLine();
        
        
        
    System.out.println("Enter the Quantity and price separated by SPACE.");
        SecondQuestion = sc.nextLine().split(" ");
        
            int Quantity1 = Integer.parseInt(SecondQuestion[0]);
            double Price1 = Double.parseDouble(SecondQuestion[1]);
            double Amount1 = 0;
            Amount1 = Quantity1 * Price1;
            int Quantity = 0 + Quantity1;
            double Amount = 0 + Amount1;
            double Price = 0 + Price1;

    gro.setGrocery(Price, Quantity, Amount);
    System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " + 
                                                                            gro.getitemPrice() + " each");

    System.out.println("Amount due is " + gro.getamountDue());
    
}   

}


Solution

  • Piling all your code into main is a bad idea. Java is object oriented, and main, being static, isn't.

    The solution for sharing data between methods is usually to make a field.

    class Main {
      public static void main(String[] args) throws Exception {
        new Main().go();
      }
    
      private Scanner scanner;
    
      void go() throws Exception {
        scanner = new Scanner(System.in);
        scanner.useDelimiter("\\R");
    
        int quantity = askInt("Enter the quantity: ");
      }
    
      private int askInt(String prompt) {
        while (true) {
          System.out.print(prompt);
          if (scanner.hasNextInt()) return scanner.nextInt();
          scanner.next(); // eat the non-int token
          System.out.println("ERROR: Please enter an integer number.");
        }
      }
    }
    

    That's what virtually all command line java apps should look like: a one-liner main method, no use of static anywhere except main, a scanner field, set with the proper delimiter (\\R, which means .nextX() always reads one line worth of input; use .next() to read an entire line. Don't call .nextLine(), ever. nextLine() and all the other next methods interact in nasty ways that make scanner useless or at least unwieldy and bugprone for command line 'prompting', which why you do it this way.