This driving me crazy! I fixed my java code and now my prompt about fundraiser g
ID: 3758234 • Letter: T
Question
This driving me crazy! I fixed my java code and now my prompt about fundraiser goes through the cases. However when I enter n for one of the prompts the next line is just a blank line when it is suppose to ask about earnings. I have included the sample output so it can be tested and the output I am getting. What am I doing wrong?
My code:
import java.util.Scanner; //Accesses Scanner Class for data input.
public class LE54
{
private static Scanner input = new Scanner(System.in); //REF Var/Object for input from the keyboard.
private static String gradeLevel = "";
public static void main(String[] args)
{
int grade = setgradeLevel();
double totalEarnings = 0;
for (int counter = 1; counter <= grade; counter++)
{
totalEarnings += setEarnings(ascertainFundraising(counter));
}
displaySeniorTripInfo(setCostOfSeniorTrip(), totalEarnings);
} //END main ()
public static int setgradeLevel()
{
int grade = 0;
do
{
System.out.printf("%n 1. Freshmen"
+ "%n 2. Sophomore"
+ "%n 3. Junior"
+ "%n 4. Senior"
+ "%n%n Enter your grade level: ");
grade = input.nextInt();
if(grade <1 || grade >4)
{
System.out.printf("%nInvalid grade level! Try again.%n");
}//end if
}while (grade < 1 || grade > 4); //end do-while
return grade;
}// end gradeLevel: static int
public static char ascertainFundraising(int counter)
{
switch (counter)
{
case 1: gradeLevel = "Freshman";
break;
case 2: gradeLevel = "Sophomore";
break;
case 3: gradeLevel = "Junior";
break;
case 4: gradeLevel = "Senior";
}
System.out.printf("%nDid you hold a fundraiser in your %s year? ", gradeLevel);
input.nextLine();
return input.nextLine().charAt(0);
}// end ascertainFundraising: static char
public static double setEarnings(char response)
{
double earnings = 0;
if (Character.toUpperCase(response) == 'Y')
{
System.out.printf("%nEnter your %s fundraiser earnings: ", gradeLevel);
earnings = input.nextDouble();
input.nextLine();
}
else
{
earnings = 0;
}
return earnings;
}//end setEarnings: static double
public static double setCostOfSeniorTrip()
{
System.out.printf("%nEnter the cost of your senior trip: ");
return input.nextDouble();
}// end setCostOfSeniorTrip: static double
public static void displaySeniorTripInfo (double costOfSeniorTrip, double earnings)
{
System.out.printf("%nEARNINGS REPORT FOR SENIOR TRIP"
+"%n%nCost of Your Senior Trip: $%,.2f"
+ "%nEarnings Year-To-Date: %,.2f"
+"%n%nDifference: $,%.2f", costOfSeniorTrip, earnings, costOfSeniorTrip-earnings);
}//end displaySeniorTripInfo: Void
}//End class
Proper output suppose to get:
Explanation / Answer
Modify the method ascertainFundraising as follows
--------------------------------------------------------------------------------------------------------------------------------------------
public static char ascertainFundraising(int counter)
{
//Declare a string variable for each call of ascertainFundraising
//so that the values will be reset to empty
String choice="";
switch (counter)
{
case 1: gradeLevel = "Freshman";
break;
case 2: gradeLevel = "Sophomore";
break;
case 3: gradeLevel = "Junior";
break;
case 4: gradeLevel = "Senior";
}
System.out.printf("%nDid you hold a fundraiser in your %s year? ", gradeLevel);
//read user input value from keyboard using next() method
choice=input.next();
//return the first character of the string choice
return choice.charAt(0);
}// end ascertainFundraising: static char
Modify the method displaySeniorTripInfo as following
--------------------------------------------------------------------------------------------------------------------------------------------
public static void displaySeniorTripInfo (double costOfSeniorTrip, double earnings)
{
//To get difference in negative , use earnings-costOfSeniorTrip
System.out.printf("%nEARNINGS REPORT FOR SENIOR TRIP"
+"%n%nCost of Your Senior Trip: $%,.2f"
+ "%nEarnings Year-To-Date: %,.2f"
+"%n%nDifference: $,%.2f", costOfSeniorTrip, earnings, earnings-costOfSeniorTrip);
}//end displaySeniorTripInfo: Void
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Total program
//LE54.java
import java.util.Scanner; //Accesses Scanner Class for data input.
public class LE54
{
private static Scanner input = new Scanner(System.in);
//REF Var/Object for input from the keyboard.
private static String gradeLevel = "";
public static void main(String[] args)
{
int grade = setgradeLevel();
double totalEarnings = 0;
for (int counter = 1; counter <= grade; counter++)
{
totalEarnings += setEarnings(ascertainFundraising(counter));
}
displaySeniorTripInfo(setCostOfSeniorTrip(), totalEarnings);
} //END main ()
public static int setgradeLevel()
{
int grade = 0;
do
{
System.out.printf("%n 1. Freshmen"
+ "%n 2. Sophomore"
+ "%n 3. Junior"
+ "%n 4. Senior"
+ "%n%n Enter your grade level: ");
grade = input.nextInt();
if(grade <1 || grade >4)
{
System.out.printf("%nInvalid grade level! Try again.%n");
}//end if
}while (grade < 1 || grade > 4); //end do-while
return grade;
}// end gradeLevel: static int
public static char ascertainFundraising(int counter)
{
//Declare a string variable for each call of ascertainFundraising
//so that the values will be reset to empty
String choice="";
switch (counter)
{
case 1: gradeLevel = "Freshman";
break;
case 2: gradeLevel = "Sophomore";
break;
case 3: gradeLevel = "Junior";
break;
case 4: gradeLevel = "Senior";
}
System.out.printf("%nDid you hold a fundraiser in your %s year? ", gradeLevel);
//read user input value from keyboard using next() method
choice=input.next();
//return the first character of the string choice
return choice.charAt(0);
}// end ascertainFundraising: static char
public static double setEarnings(char response)
{
double earnings = 0;
if (Character.toUpperCase(response) == 'Y')
{
System.out.printf("%nEnter your %s fundraiser earnings: ", gradeLevel);
earnings = input.nextDouble();
input.nextLine();
}
else
{
earnings = 0;
}
return earnings;
}//end setEarnings: static double
public static double setCostOfSeniorTrip()
{
System.out.printf("%nEnter the cost of your senior trip: ");
return input.nextDouble();
}// end setCostOfSeniorTrip: static double
public static void displaySeniorTripInfo (double costOfSeniorTrip, double earnings)
{
//To get difference in negative , use earnings-costOfSeniorTrip
System.out.printf("%nEARNINGS REPORT FOR SENIOR TRIP"
+"%n%nCost of Your Senior Trip: $%,.2f"
+ "%nEarnings Year-To-Date: %,.2f"
+"%n%nDifference: $,%.2f", costOfSeniorTrip, earnings, earnings-costOfSeniorTrip);
}//end displaySeniorTripInfo: Void
}//End class
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
1. Freshmen
2. Sophomore
3. Junior
4. Senior
Enter your grade level: 3
Did you hold a fundraiser in your Freshman year? N
Did you hold a fundraiser in your Sophomore year? Y
Enter your Sophomore fundraiser earnings: 5000
Did you hold a fundraiser in your Junior year? Y
Enter your Junior fundraiser earnings: 7000
Enter the cost of your senior trip: 20000
EARNINGS REPORT FOR SENIOR TRIP
Cost of Your Senior Trip: $20,000.00
Earnings Year-To-Date: 12,000.00
Difference: $,-8000.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.