Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Help writing the statement for Y/N

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-09-2012, 01:33 AM PM User | #1 limeleaf New to the CF scene   Join Date: Mar 2012 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts Help writing the 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: 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. Please help, I appreciate any input! Last edited by Fou-Lu; 03-09-2012 at 04:46 AM..
 03-09-2012, 05:00 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,597 Times in 2,566 Posts 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.
03-09-2012, 07:32 AM   PM User | #3
limeleaf
New to the CF scene

Join Date: Mar 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Fou-Lu 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").

Are you saying that I need to create 2 cases:

if s.equalsIgnoreCase("y") ...?

what do I do next?

03-09-2012, 07:43 AM   PM User | #4
limeleaf
New to the CF scene

Join Date: Mar 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by limeleaf Can you please show in my example how to do this - .equalsIgnoreCase("y"). Are you saying that I need to create 2 cases: if s.equalsIgnoreCase("y") ...? what do I do next? thanks for your help!!!
You also mentioned that I have " lot of comparison mismatches between strings and chars" - where do I have chars and how do I fix this?

thank you

 03-09-2012, 09:16 AM PM User | #5 limeleaf New to the CF scene   Join Date: Mar 2012 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts Okay, here is where i got so far: 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)); } } 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.
 03-09-2012, 01:01 PM PM User | #6 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,597 Times in 2,566 Posts 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.
03-09-2012, 10:05 PM   PM User | #7
limeleaf
New to the CF scene

Join Date: Mar 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Fou-Lu 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!
Attached Thumbnails

 03-09-2012, 10:18 PM PM User | #8 limeleaf New to the CF scene   Join Date: Mar 2012 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts I see what I did wrong - I didn't clear the buffer every time, now I did it, and it is fixed. One more thing - when it calculates laps, it doesn't do the count right. I enter 2, and it puts out 1... then when I put N to show that company is not a sponsor, it still counts total over \$100. Also, I keep seeing that error so it seems that buffer need to be cleaned more: Last edited by limeleaf; 03-09-2012 at 10:45 PM..
 03-09-2012, 11:38 PM PM User | #10 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,599 Thanks: 4 Thanked 2,597 Times in 2,566 Posts 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).
 Users who have thanked Fou-Lu for this post: limeleaf (03-21-2012)

 Bookmarks

 Tags conditional, if statement, looping

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:40 PM.