Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help writing the IF statement for Y/N

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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:
    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
    }
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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 ==.)
    Last edited by JonLane; March 7th, 2012 at 08:18 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    if(<THE CONDITION TEST HERE>) {
      // some statements to execute
    }

    Note <THE CONDITION TEST HERE> needs to resolve to true or false.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help writing the IF statement for Y/N

    For examples try Google or do a Search here.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!
    Last edited by limeleaf; March 9th, 2012 at 04:28 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

    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

Similar Threads

  1. [SOLVED] Writing into ViewBuffers!! need help
    By Anoop Shiralige in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 19th, 2012, 05:38 AM
  2. Applet Not Writing to FTP
    By jtd200 in forum Java Networking
    Replies: 9
    Last Post: January 13th, 2012, 12:25 PM
  3. Writing to DTD Document
    By DanG1775 in forum Java SE APIs
    Replies: 4
    Last Post: December 12th, 2011, 04:41 PM
  4. Writing to files
    By nitwit3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2011, 04:00 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM

Tags for this Thread