Results 1 to 5 of 5
  1. #1
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

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

  2. #2
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    I didn't run the code but i think u should put the exchange.menu(); inside do-while

    Quote Originally Posted by JDAWG View Post
    Currently Lab7 when u promoted is there another estimate and user selects y for yes it just hangs up.
    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();
      }
    }

  3. #3
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    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 choice
    Last edited by JDAWG; 03-14-2009 at 06:50 AM.

  4. #4
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    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;
    	}
    	
    }

  5. #5
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by JDAWG View Post
    heres some new code that should work but doesnt... when Y is selected it just sits there.... anyone know?
    what happens if u click 'Y' and than u select 1, or 2???...After u select 'Y' the first called is exchange.choice();, so the programm expects from u to make the choise

Similar Threads

  1. Syntax for choice box(combo box)
    By NickkicN in forum New To Java
    Replies: 2
    Last Post: 08-14-2008, 10:26 PM
  2. String array for Choice, JOptionPane doesn't show
    By themburu in forum Java Applets
    Replies: 5
    Last Post: 05-29-2008, 01:10 PM
  3. Need help with T/F and Multiple Choice
    By sayso36 in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 04:39 PM
  4. Adding file contents to Choice component
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:06 AM
  5. Using java.awt.Choice
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •