Help writing the IF statement for Y/N
Hi all,
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:
.......
import java.util.Scanner; // Import the Scanner Class Library
import java.text.*;
public class Exam
{
public static void main(String args[])
{
// variable declaration
String s;
int st_name, spon_name, laps, amountSchool, count = 0;
double dolla;
double total = 0;
Scanner Keyboard = new Scanner(System.in); //Create Scanner object "Keyboard"
/ Print Title
System.out.print( "\t\t\tMy Jog-A-Thon Fundraising Activity\n\n" );
// Prompt for student name
System.out.println("\tEnter student's name: ");
st_name = Keyboard.nextInt();
System.out.print("\nEnter sponsor's name: ");
spon_name = Keyboard.nextInt();
// 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.nextDouble();
// 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 = Keyboard.nextLine();
choice = s.charAt(0);
...........................
And I am stuck from here on. I need to find total, if s = n or N, total needs to be no more than 100, but if s = y or Y, I can have any total.
Please help, I appreciate any input!
Re: Help writing the IF statement for Y/N
The String class has methods to see if two Strings are equal while ignoring the Strings' case. Look at the API doc for the String class.
Java Platform SE 6
Re: Help writing the IF statement for Y/N
Firstly, why are your student and sponsor names integers?
As for if-statements, they have this general syntax:
Code Java:
if(booleanExpression) {
//statements to execute here
} else if(booleanExpression) {
//statements to execute here
} else {
//statements to execute here
}
//To compare ints
int pie = 7;
if(pie == 2) {
//stuff to do here
}
//To compare chars
char c = 'h'
if(c == 'h') {
//stuff to do here
}
Re: Help writing the IF statement for Y/N
to save you the trouble later in your coding career,
if you want a true or false responce (called a boolean) from comparing two Strings, == will not work because it compares the LINK/Pointer of the String Objects. using == operator on any OBJECT is asking if they are the exact same OBJECT not Objects of exact same data.
so ask about the objects data, the .equals() method is used, and is defined for Strings.
String1.equals(String2); will return a true if the string is identical, and false if it is not.
I believe you may also want to use String1.equalsIgnoreCase(String2) (not sure if that is the exact method name but something very close to that)
happy coding,
Jonathan
(Note: chars are primitive types and can use ==.)
Re: Help writing the IF statement for Y/N
Can somebody show me an example of what is being described in theory here? In my particular assignment, can somebody please show how to write out the two cases (if user types Y, I have to total the amount, if N - total, but no more than to $100. also, I need to count for both upper case and lower case submissions.
I would appreciate a practical explanation. thank you
Re: Help writing the IF statement for Y/N
To test if a String variable: str content's is "Y" use: str.equals("Y")
To test if a char variable: aChar content's is 'N' use: aChar == 'N'
Use an if statement to conditionally execute some statements:
Code :
if(<THE CONDITION TEST HERE>) {
// some statements to execute
}
Note <THE CONDITION TEST HERE> needs to resolve to true or false.
Re: Help writing the IF statement for Y/N
oh my... I guess I am in the wrong forum... I was asking fro some practical example based on my assignment. Because I udnerstand all that is said above, but ic an't put it together for my specific assignment...
Re: Help writing the IF statement for Y/N
For examples try Google or do a Search here.
Re: Help writing the IF statement for Y/N
Okay, here is where i got so far:
[code=java]
<
import java.util.Scanner; // Import the Scanner Class Library
import java.text.*;
import java.lang.*;
public class Lab6_v2
{
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, total = 0, amountSchool;
char choice, m;
Scanner Keyboard = new Scanner(System.in); //Create Scanner object "Keyboard"
DecimalFormat df = new DecimalFormat("###,##0.##");
// Print Title
System.out.println( "\t\t\tMy Jog-A-Thon Fundraising Activity\n\n" );
do
{
// Prompt for student name
System.out.print("\tEnter student's name: ");
stName = Keyboard.next();
// Student name to upper case
Uname_st = stName.toUpperCase;
System.out.print("\nEnter sponsor's name: ");
spon_name = Keyboard.next();
// 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));
}
}
>
[/code=java]
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.
Thanks for everybody's input!
Re: Help writing the IF statement for Y/N
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Quote:
cannot find symbol
symbol : variable toUpperCase
location: class java.lang.String
Are you trying to reference a field in String or call the method with that name? Method references end with ()s