Forgive me if my questions are too silly. I am a Java beginner (very much of a beginner), and I received the following assignment:
"Many public schools in California are experiencing a shortage of funds for special student activities. Some schools started Jog-A-Thons as a fundraiser. Each student gathers sponsors for a particular amount per lap jogged. Write a program that tracks this activity.
The program repeats until all sponsors are entered. It accumulates the grand total funds owed the school. For each sponsor, the program prompts for the name of the student. It then prompts for the sponsors’ name, and amount pledged for each lap. The program asks if the sponsor is a company (Y/N). Then the program prompts for the amount of laps the student actually jogged. The program calculates the amount owed to the school (pledge times laps). If the sponsor is not a company, the maximum amount owed the school is set to $100.00, (There is no maximum if the sponsor is a company). Finally, display on the screen the students name and sponsor name changed to all uppercase (whether or not it was entered in upper case), the pledge amount, the number of laps, and the amount calculated. Prompt if there is another sponsor (Y/N). After all sponsors are entered successfully, display the grand total funds owed the school. All money must be formatted appropriately."
So, I am stuck at figuring out how to write at the Y/N cases.
here is what I've got so far:
Code:
/* Andrew B
Test
03/01/2012
This program uses
System.out.println for standard output */
import java.util.Scanner; // Import the Scanner Class Library
import java.text.*;
public class Test
{
public static void main(String args[])
{
// variable declaration
int amountSchool, laps, count = 0;
String s, , more_sp, st_name, spon_name, Uname_st, Uname_spon;
float dolla, laps, total = 0;
Scanner input = new Scanner(System.in); //Create Scanner object "input"
DecimalFormat df = new DecimalFormat("###,##0.##");
// Print Title
System.out.print( "\t\t\tMy Jog-A-Thon Fundraising Activity\n\n" );
do
{
// Prompt for student name
System.out.println("\tEnter student's name: ");
st_name = input.nextLine();
// Student name to upper case
Uname_st = st_name.toUpperCase;
System.out.print("\nEnter sponsor's name: ");
spon_name = input.nextLine();
// Sponsor name to upper case
Uname_spon = spon_name.toUpperCase;
// Ask For Pledge Amount
System.out.print( "\n\n\n\t\t\tEnter U.S. dollar amount pledged for each lap: \n\t\t\t" );
dolla = input.nextFloat();
// Ask If Sponsor is a Company
System.out.print( "\n\n\n\t\t\tIs the sponsor a company (Y/N)?\n\t\t\t" );
System.out.print( "\n\n\n\t\t\tPlease type Y for 'yes' and N for 'no'\n\t\t\t" );
s = input.nextLine();
choice = s.charAt(0);
if (s == 'y' || s == 'Y')
{
System.out.print( "\n\n\n\t\t\tEnter the number of laps student has jogged:\n\t\t\t" );
laps = input.nextFloat();
amountSchool = laps* dolla;
System.out.println( "\n\t\t\tAmount owed to school is $" + df.format(amountSchool));
}
else if (s == 'n' || s == 'N')
{
System.out.print( "\n\n\n\t\t\tEnter the number of laps student has jogged:\n\t\t\t" );
laps = input.nextFloat();
amountSchool = laps* dolla;
if (amountSchool > 100)
{
System.out.println( "\n\t\t\tAmount owed to school is $100");
}
else
{
System.out.println( "\n\t\t\tAmount owed to school is $" + df.format(amountSchool));
}
}
else
{
System.out.println( "\n\t\t\tInvalid character");
count++;
total += amountSchool;
}
}
System.out.print("\nStudent's name: " + st_name);
System.out.print("\nSponsor's name: " + spon_name);
System.out.print( "\nAmount pledged for each lap: \n\t\t\t" + de.format(dolla));
System.out.println("\nTotal laps = " + count);
System.out.println("\nTotal amount owed to school= $" + df.format(total));
System.out.print( "\n\n\n\t\t\tIs tthere another sponsor (Y/N)?\n\t\t\t" );
more_spon = input.nextLine();
}
while (more_spon == 'y' || more_spon == 'Y');
System.out.println("\nTotal amount owed to school=$" + df.format(total));
}
}
I need to keep repeating this program until all sponsors are entered. I am confused with all the repetitions, since I already used them for other reason, please help.
The loop itself requires the while loop before the printing of the student name and whatnot. That is where the closing brace for the do{} is.
There is a lot more wrong than this though. You have undeclared variables, and a lot of comparison mismatches between strings and chars. Personally I wouldn't bother with the chars, just use strings and compare them with .equalsIgnoreCase("y") instead; strings cannot be reasonably compared to using == operators, as this compares the immutable values together ("y" == "y", but s = new String("y") != "y"), so as objects always compare them using .equals (or equalsIgnoreCase). Also, variables used for control structures such as a do/while will need to be declared before the loop in order to compare the value within the condition; if its created within the do its not scoped for visibility in the while.
The loop itself requires the while loop before the printing of the student name and whatnot. That is where the closing brace for the do{} is.
There is a lot more wrong than this though. You have undeclared variables, and a lot of comparison mismatches between strings and chars. Personally I wouldn't bother with the chars, just use strings and compare them with .equalsIgnoreCase("y") instead; strings cannot be reasonably compared to using == operators, as this compares the immutable values together ("y" == "y", but s = new String("y") != "y"), so as objects always compare them using .equals (or equalsIgnoreCase). Also, variables used for control structures such as a do/while will need to be declared before the loop in order to compare the value within the condition; if its created within the do its not scoped for visibility in the while.
Can you please show in my example how to do this - .equalsIgnoreCase("y").
// Sponsor name to upper case
Uname_spon = spon_name.toUpperCase;
// Ask For Pledge Amount
System.out.print( "\n\n\n\t\t\tEnter U.S. dollar amount pledged for each lap: \n\t\t\t" );
dolla = Keyboard.nextFloat();
// Ask If Sponsor is a Company
System.out.print( "\n\n\n\t\t\tIs the sponsor a company (Y/N)?\n\t\t\t" );
System.out.println( "\n\n\n\t\t\tPlease type Y for 'yes' and N for 'no'\n\t\t\t" );
s = Keyboard.nextLine();
choice = s.charAt(0);
if (choice == 'y' || choice == 'Y')
{
System.out.print( "\n\n\n\t\t\tEnter the number of laps student has jogged:\n\t\t\t" );
laps = Keyboard.nextFloat();
amountSchool = laps* dolla;
System.out.println( "\n\t\t\tAmount owed to school is $" + df.format(amountSchool));
}
else if(choice == 'n' || choice == 'N')
{
System.out.print( "\n\n\n\t\t\tEnter the number of laps student has jogged:\n\t\t\t" );
laps = Keyboard.nextFloat();
amountSchool = laps* dolla;
if (amountSchool > 100)
{
System.out.println( "\n\t\t\tAmount owed to school is $100");
}
else
{
System.out.println( "\n\t\t\tAmount owed to school is $" + df.format(amountSchool));
}
}
else
{
System.out.println( "\n\t\t\tInvalid entry");
}
count++;
total += amountSchool;
System.out.println("\nStudent's name: " + stName);
System.out.println("\nSponsor's name: " + spon_name);
System.out.println( "\nAmount pledged for each lap: \n\t\t\t" + df.format(dolla));
System.out.println("\nTotal laps = " + count);
System.out.println("\nTotal amount owed to school= $" + df.format(total));
System.out.print( "\n\n\n\t\t\tIs tthere another sponsor (Y/N)?\n\t\t\t" );
more_spon = Keyboard.nextLine();
m = more_spon.charAt(0);
}
while (m == 'y' || m == 'Y');
System.out.println("\nTotal amount owed to school=$" + df.format(total));
}
}
Now, here is the mistake I keep getting OVER AND OVER again, I can't figure it out, can anybody?
Lab6_v2.java:41: cannot find symbol
symbol : variable toUpperCase
location: class java.lang.String
Uname_st = stName.toUpperCase;
^
Lab6_v2.java:48: cannot find symbol
symbol : variable toUpperCase
location: class java.lang.String
Uname_spon = spon_name.toUpperCase;
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Cannot find symbol is the same as saying this variable / property doesn't exist. .toUpperCase doesn't exist in String, but .toUpperCase() does. Change those from properties to methods. Then you just need to initialize the variable for amountSchool and it will compile.
Using the scanner with next# type calls will require you to flush the buffer. Call Keyboard.nextLine() after any fetch to a number to clear the linefeed off of the buffer.
Cannot find symbol is the same as saying this variable / property doesn't exist. .toUpperCase doesn't exist in String, but .toUpperCase() does. Change those from properties to methods. Then you just need to initialize the variable for amountSchool and it will compile.
Using the scanner with next# type calls will require you to flush the buffer. Call Keyboard.nextLine() after any fetch to a number to clear the linefeed off of the buffer.
Thank you, Fou-Lu. I fixed my mistakes as you said and it compiled. Now I am having an error when I run the application - when I entered "10" for dollar amount, I got an error saying "exception in thread "main" ... - string index out of range 0"
can you please look at the attached image? thanks!
Thanks again, I fixed my code, looking good now except for one little thing: how can I get so that after I asked if there is another sponsor, screen gets clean again?
I appreciate you looking at this, your explanations actually make a lot of sense, thank you:
import java.util.Scanner; // Import the Scanner Class Library
import java.text.*;
import java.lang.*;
public class Exam1
{
public static void main(String[] args)
{
// variable declaration
int count = 0;
String more_spon, stName, spon_name, Uname_st, Uname_spon, s;
float dolla, laps = 0, total = 0, amountSchool = 0;
char choice, m;
Scanner Keyboard = new Scanner(System.in); //Create Scanner object "Keyboard"
NumberFormat dollar = NumberFormat.getCurrencyInstance();
Clearing the console isn't really doable in Java. Since its designed for cross platform support, hooking into the console just wasn't something that they put that much effort into.
I recall the escape sequence ("\033[2J") used to work, but I just tried it and it did not clear the console. Sadly 100 empty lines seems to be the best route for console clear in Java (avoid a system call whenever possible).
Okay, I received a new assignment, and I am a bit lost again:
Modify Lab 6 by adding static methods and one static variable. In your Foreign class, create two static variables: change your menu choice (int) to static, and add a new variable: count (int) to count the number of conversions made. Create 3 static methods in your Foreign class: one to display a title “Foreign Exchange”, one to display the menu (you already have this one but make it static, and one to display the count. Call the “titles” method and the “menu” method before any objects are created using the class name. Increment your count within the method that assigns the appropriate country and rate. Call the “display count” method after the user selects quit from your menu.
And here are my Lab6 files that assignment refers to:
File "Foreign":
import java.util.Scanner;
import java.text.*;
public class Foreign
{
private int menuChoice;
private double convRate;
private double dollars;
private double convAmount;
private String country;
private DecimalFormat fmt = new DecimalFormat("###,##0.00");
// initialize data;
public Foreign()
{
convRate = 0;
dollars = 0;
convAmount = 0;
String country = null;
}
// display the menu and get a choice;
public void processMenu()
{
Scanner keyboard = new Scanner(System.in);
// display the data horizontally with one space between values
@Override
public String toString()
{
return "\n\t" + country + " " +
fmt.format(convRate) + " " +
fmt.format(dollars) + " " +
fmt.format(convAmount);
}
}
And second file, the actual program Lab6:
public class Lab6
{
public static void main(String args[])
{
// create a Foreign object
Foreign f = new Foreign();
do // main loop
{
// process menu
f.processMenu();
// get menu choice - if quit
if(f.getMenuChoice() == 0)
break;
// get dollar amount and calculate the converted amount
f.getDollars();
f.calculate();
// display results vertically and horizontally
f.displayVertically();
System.out.println(f);