Home > Back-end >  I am trying to use scanner outside the main method, and it keeps repeating after 3 inputs
I am trying to use scanner outside the main method, and it keeps repeating after 3 inputs

Time:10-30

So I am trying to use scanner outside main method and keeps on repeating on enter, and idk if the new Obj is causing it cause I observed that the number of obj I created is the same as the amount of times I repeated input process and enter. Then after those tries it will then show the output inside the main method.

This is my code:

package galitkami;

import java.util.Scanner;

public class test {

    Scanner myObj = new Scanner(System.in);
    {System.out.print("Enter Carat Value: ");}
    int c = myObj.nextInt();
    double g = 0.20;
    double cg = c*g;
    double gm = cg*1000;
    double mg = gm*0.00220462/1000;
    public static void main(String[] args) {

        test a = new test();
        test b = new test();
        test c = new test();
       
        System.out.println("Carats in Grams: " a.cg);
        System.out.println("Grams in Milligrams: " b.gm);
        System.out.println("Milligrams in Pounds: " c.mg);

    }
    
}

I am expecting that after I input value on "Enter Carat Value:" it will automatically show the output inside the main method.

Update, I have found a solution for this and this is what it looks like:

import java.util.Scanner;

public class test {

    static Scanner myObj = new Scanner(System.in);
    {System.out.print("Enter Carat Value: ");}
    final int c = myObj.nextInt();
   
    double g = 0.20;
    double cg = c*g;
    double gm = cg*1000;
    double mg = gm*0.00220462/1000;
    public static void main(String[] args) {

        test a = new test();
   

       
      
        System.out.println("Carats in Grams: " a.cg);
        System.out.println("Grams in Milligrams: " a.gm);
        System.out.println("Milligrams in Pounds: " a.mg);
    

   
}   
}

My friend told me that I have simply need to create one object and call it out for all.

CodePudding user response:

I don't know your level of expertise in programming and Java. However there are a few things that I believe are strongly recommended to follow. Don't use a Scanner outside a method. Never print outside a method. Initiate all class attributes that aren't immutable inside a class constructor call.

In your current code you have three calls to new test(); where each call will initiate all class attributes and hence the line int c = myObj.nextInt(); will be executed three times.

Instead create a constructor method in your Test class. Initiate all your attributes here and execute all System.out prints here or in the main method.

public class Test {
    //Declare attributes only
    int c;
    double g, cg, gm, mg;

    //Class constructor method
    public Test(Scanner myObj) {
        System.out.print("Enter Carat Value: ");
        //Give declared attributes their values
        c = myObj.nextInt();
        g = 0.20;
        cg = c*g;
        gm = cg*1000;
        mg = gm*0.00220462/1000;
        
        System.out.println("Carats in Grams: "  cg);
        System.out.println("Grams in Milligrams: "  gm);
        System.out.println("Milligrams in Pounds: "  mg);
    }
    
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in);

        //Calling new Test() executes the constructor method above
        new Test(myObj);
        
        myObj.close();
    }
}
  • Related