Results 1 to 5 of 5
- 03-13-2009, 11:59 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
having issue rerunning code if choice was Y for yes
I have 2 sets of code the first one called Lab7 uses Foreign to run. Currently Lab7 errors out saying
Lab7.java:23: variable exchange might not have been initialized
exchange.getChoice();
Looks like its going to error out on any of my exchange.(whatever) code. I am looking for any advice with that lab thanks for any help ahead of time.
Java Code:import java.util.Scanner; import java.text.NumberFormat; public class Lab7 { public static void main(String[] args) { String more = "Y"; Scanner Keyboard = new Scanner(System.in); Foreign exchange; Foreign.title(); Foreign.menu(); do { exchange.getChoice(); exchange.getDollars(); exchange.convAmount(); exchange.showVertical(); System.out.println("\n" + exchange); System.out.print("\nEnter another estimate (y/n): "); more = Keyboard.nextLine(); } while (Character.toUpperCase(more.charAt(0)) == 'Y'); Foreign.counter(); } }Java Code:import java.util.Scanner; import java.text.NumberFormat; public class Foreign { private static int choice, count; private String country; private double dollars, rate, amount; Scanner Keyboard = new Scanner(System.in); NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(); public static void title() { System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); System.out.println(" Foreign Exchange"); System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n"); } public static void counter() { System.out.println("\nThe total count is " + count); } public Foreign() { count = 0; dollars = 0.0; rate = 0.0; amount = 0.0; country = "null"; } public static void menu() { System.out.println("======================================================"); System.out.println(" Foreign Exchange Menu"); System.out.println("======================================================\n\n"); System.out.println("1. U.S. to Canada"); System.out.println("2. U.S. to Mexico"); System.out.println("3. U.S. to Japan"); System.out.println("4. U.S. to Euro"); System.out.print("\nPlease enter your choice:"); } public void choice() { choice = Keyboard.nextInt(); } public void dollars() { System.out.print("\nEnter the amount of U.S. Dollars you want to convert: "); dollars = Keyboard.nextDouble(); if (choice >= 1 && choice <= 4) switch (choice) { case 1: rate = 1.2418; country = "Canadian Dollars"; break; case 2: rate = 14.5436; country = "Mexican Pesos"; break; case 3: rate = 88.9183; country = "Japanese Yen"; break; case 4: rate = 0.774078; country = "Euros"; break; } amount = dollars * rate; count++; } public double convAmount() { return amount; } public double getDollars() { return dollars; } public int getChoice() { return choice; } public void showVertical() { System.out.println("\nCountry = " + country); System.out.println("Rate = " + rate); System.out.println("Dollars = " + dollarFormat.format(dollars)); System.out.println("Value = " + amount); } public String toString() { String line; line = country + " " + rate + " " + dollarFormat.format(dollars) + " " + amount; return line; } }Last edited by JDAWG; 03-14-2009 at 06:49 AM.
- 03-14-2009, 12:49 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
I didn't run the code but i think u should put the exchange.menu(); inside do-while
Java Code:import java.util.Scanner; import java.text.NumberFormat; public class Lab7 { public static void main(String[] args) { String another = "Y"; Foreign exchange = new Foreign(); Scanner Keyboard = new Scanner(System.in); exchange.title(); do { [B]exchange.menu();[/B] exchange.choice(); exchange.dollars(); exchange.vertical(); System.out.println("\n" + exchange); System.out.print("\nEnter another sign (y/n): "); another = Keyboard.nextLine(); } while (Character.toUpperCase(another.charAt(0)) == 'Y'); exchange.menu(); Foreign.counter(); } }
- 03-14-2009, 01:58 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
My instructor requested :
Call the “titles” method and the “menu” method before any objects are created using the class name.
So I took that as I need to call the title method I created and the menu method I created before the do while. Am I not understanding that? but I still have the question which lab i should use to run the Foreign. Lab6 looks cleaner but doesnt run correctly... So maybe I should just take out Lab7 and work on Lab6 and Foreign why doesnt it run correctly? It just runs and shows the count it doesnt call any of the methods to have a choiceLast edited by JDAWG; 03-14-2009 at 06:50 AM.
- 03-14-2009, 07:13 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
heres some new code that should work but doesnt... when Y is selected it just sits there.... anyone know?
Java Code:import java.util.Scanner; import java.text.NumberFormat; public class Lab7 { public static void main(String[] args) { String more = "Y"; Foreign exchange = new Foreign(); Scanner Keyboard = new Scanner(System.in); exchange.title(); exchange.menu(); do { exchange.choice(); exchange.dollars(); exchange.vertical(); System.out.println("\n" + exchange); System.out.print("\nWould you like to enter another exchange? Y/N "); more = Keyboard.nextLine(); } while (Character.toUpperCase(more.charAt(0)) == 'Y'); Foreign.counter(); }Java Code:import java.util.Scanner; import java.text.NumberFormat; public class Foreign { private static int choice, count; private String country; private double dollars, rate, amount; Scanner Keyboard = new Scanner(System.in); NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(); public static void title() { System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); System.out.println(" Foreign Exchange"); System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n"); } public static void counter() { System.out.println("\nThe total count is " + count); } public Foreign() { count = 0; dollars = 0.0; rate = 0.0; amount = 0.0; country = "null"; } public static void menu() { System.out.println("======================================================"); System.out.println(" Foreign Exchange Menu"); System.out.println("======================================================\n\n"); System.out.println("1. U.S. to Canada"); System.out.println("2. U.S. to Mexico"); System.out.println("3. U.S. to Japan"); System.out.println("4. U.S. to Euro"); System.out.print("\nPlease enter your choice:"); } public void choice() { choice = Keyboard.nextInt(); } public int getChoice() { return choice; } public void dollars() { System.out.print("\nEnter the amount of U.S. Dollars you want to convert: "); dollars = Keyboard.nextDouble(); if (choice >= 1 && choice <= 4) switch (choice) { case 1: rate = 1.2418; country = "Canadian Dollars"; break; case 2: rate = 14.5436; country = "Mexican Pesos"; break; case 3: rate = 88.9183; country = "Japanese Yen"; break; case 4: rate = 0.774078; country = "Euros"; break; } amount = dollars * rate; count++; } public void vertical() { System.out.println("\nCountry = " + country); System.out.println("Rate = " + rate); System.out.println("Dollars = " + dollarFormat.format(dollars)); System.out.println("Value = " + amount); } public String toString() { String line; line = country + " " + rate + " " + dollarFormat.format(dollars) + " " + amount; return line; } }
- 03-14-2009, 10:36 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Syntax for choice box(combo box)
By NickkicN in forum New To JavaReplies: 2Last Post: 08-14-2008, 10:26 PM -
String array for Choice, JOptionPane doesn't show
By themburu in forum Java AppletsReplies: 5Last Post: 05-29-2008, 01:10 PM -
Need help with T/F and Multiple Choice
By sayso36 in forum Advanced JavaReplies: 0Last Post: 03-12-2008, 04:39 PM -
Adding file contents to Choice component
By Java Tip in forum Java TipReplies: 0Last Post: 02-07-2008, 09:06 AM -
Using java.awt.Choice
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks