T Brill T Brill - 2 months ago 23
Java Question

System.out.println and Scanner input.nextint() is called twice

Every time I run my java application in Eclipse the System.out.println and the Scanner input is called twice. After testing, the first input it receives is used, and the second input is not. Does anyone have a suggestion to only ask for input once? Here is my code:

import java.util.Scanner;
public class CarlysEventPrice2
{
static int guestAmt = guestNumber();
public static void main(String[] args)
{
guestNumber();
companyMotto();
guestAndPrice(guestAmt);
}

public static int guestNumber()
{
Scanner input = new Scanner(System.in);
System.out.println("How many guests will attend this event?");
int guestAmt = input.nextInt();
return guestAmt;
}

public static void companyMotto()
{
System.out.println("*************************************************");
System.out.println("* Carly's makes the food that makes it a party. *");
System.out.println("*************************************************");
}

public static void guestAndPrice(int guestAmt)
{
int GUESTPRICE = 35;
int totalPrice = GUESTPRICE * guestAmt;
boolean largeEvent = (guestAmt >= 50);
System.out.println("There will be " + guestAmt + " guests at $" + GUESTPRICE + ", equaling a total of $" + totalPrice + ".");
System.out.println("The job is classified as a large event: " + largeEvent + ".");
}
}

Answer
package org.tat.stackoverflow;

import java.util.Scanner;
public class CarlysEventPrice2
{
    public static void main(String[] args)
    {
        int guestAmt = guestNumber();
        companyMotto();
        guestAndPrice(guestAmt);
    }

    public static int guestNumber()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("How many guests will attend this event?");
        int guestAmt = input.nextInt();
        return guestAmt;
    }

    public static void companyMotto()
    {
        System.out.println("*************************************************");
        System.out.println("* Carly's makes the food that makes it a party. *");
        System.out.println("*************************************************");
    }

    public static void guestAndPrice(int guestAmt)
    {
        int GUESTPRICE = 35;
        int totalPrice = GUESTPRICE * guestAmt;
        boolean largeEvent = (guestAmt >= 50);
        System.out.println("There will be " + guestAmt + " guests at $" + GUESTPRICE + ", equaling a total of $" + totalPrice + ".");
        System.out.println("The job is classified as a large event: " + largeEvent + ".");
    }
}

The problem was static int guestAmt = guestNumber();