Question

I am almost done with this code, but for some reason I am still getting an...

I am almost done with this code, but for some reason I am still getting an error message:

Some of the instructions were:

Using a loop, prompt for a currency to find.

Inside the loop that manages the input, call the lookUpCurrency() method can pass the inputted currency code value.

The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the code in the array. If found, return true - otherwise return false

Look at the showCurrencies() method for a example of how to loop for each element.

In the findMyCurrency() method

   If found - Display the currency code and index where it was found - i.e. "FJD found at index 0".

   If not found - Display "Currency Code not found".

  
b. Prompt with "Retry Y/N ?" to try again. If I select N, then exit the loop. Allow any other char to restart the loop and prompt for a currency to find.

==================================================================

import java.util.Currency;
import java.util.Iterator;
import java.util.Set;
import java.util.Scanner;


public class CheckCurrencyCode {

  
// Sets the MAX_CURRENCIES value from the Currency Object
static int MAX_CURRENCIES = Currency.getAvailableCurrencies().size();
static String[] CURRENCY_CODES = new String[MAX_CURRENCIES];
  
  
/**
* This method will load the currency_Codes array with 3 char codes.
* It does not need to change
*/

  
private static void loadCurrencyCodes() {
Set<Currency> currencies = Currency.getAvailableCurrencies();
Iterator<Currency> i = currencies.iterator();
int idx = 0;
while(i.hasNext()) {
CURRENCY_CODES[idx++] = i.next().getCurrencyCode();
}
}
  
/**
* Raw display of the Currency_codes;
* Could also use System.out.println(Arrays.toString(CURRENCY_CODES));
* It does not need to change
*/

private static void showCurrencies() {
       System.out.println("The valid currency codes are:");
for (int idx = 0; idx < MAX_CURRENCIES;++idx) {
System.out.print(CURRENCY_CODES[idx]+",");
}
  
System.out.println();
//System.out.println(Arrays.toString(CURRENCY_CODES));
}
  
  
/**
* Look up the given currency here and set the return value
* to true if found or false if not found
*
* Hint: Use a FOR loop to traverse the loop
*
* @param myCurrencyToFind
* @return
*/

private static void lookUpCurrency(String myCurrencyToFind) {

// An boolean variable to keep track of whether the currency is found or not

       boolean currencyFound = false;
       // For loop to iterate over CURRENCY_CODES array
       for(int i=0;i<MAX_CURRENCIES;i++)
{
   // If the currency entered by user is equal to the currency at index i, break the loop
   if(CURRENCY_CODES[i].equals(myCurrencyToFind))
   {
       System.out.printf("%s was found at index %d\n",CURRENCY_CODES[i],i);

// Currency is found
       currencyFound = true;
                       break;
   }
}
  
// If the currency is not found in the array, then print that it's not found
if(!currencyFound)
{
                   System.out.println("Currency Code not found");
               }
}

  
// If the currency is not found in the array
               System.out.println("Currency Code not found");
}
  
/**
* Prompt for a currency to find
* Call the lookUpCurrency with the inputted currency code
* Display the found result
* Prompt to try again and exit this method only if the choice is to not continue
*/

  
private static void findMyCurrency()
{
   // Scanner object to read input
   Scanner s = new Scanner(System.in);
   // Choice variable to store the user choice
   char choice = 'Y';
   // Until the user choice is -1, this loop is iterated
   while(choice!='N')
   {
       // Prompting the user to enter the currency
       System.out.println("Enter the currency needed to be searched");
       // Reading the input
       String userCurrency = s.nextLine();
       // Calling the method lookUpCurrency
               lookUpCurrency(userCurrency);
      
       // Prompting the user to enter the choice
       System.out.println("Retry Y/N ?");
       // Reading the user choice
       choice = s.nextLine().charAt(0);
   }
}
  
public static void main(String[] args) {
       // Loading the currency codes
loadCurrencyCodes();
// Showing the currency codes to the user
showCurrencies();
// Finding the currency entered by the user in the currency codes array
findMyCurrency();
System.out.println("The program has been terminated");

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Currency;
import java.util.Iterator;
import java.util.Set;
import java.util.Scanner;

public class CheckCurrencyCode {

// Sets the MAX_CURRENCIES value from the Currency Object
        static int MAX_CURRENCIES = Currency.getAvailableCurrencies().size();
        static String[] CURRENCY_CODES = new String[MAX_CURRENCIES];

        /**
         * This method will load the currency_Codes array with 3 char codes. It does not
         * need to change
         */

        private static void loadCurrencyCodes() {
                Set<Currency> currencies = Currency.getAvailableCurrencies();
                Iterator<Currency> i = currencies.iterator();
                int idx = 0;
                while (i.hasNext()) {
                        CURRENCY_CODES[idx++] = i.next().getCurrencyCode();
                }
        }

        /**
         * Raw display of the Currency_codes; Could also use
         * System.out.println(Arrays.toString(CURRENCY_CODES)); It does not need to
         * change
         */
        private static void showCurrencies() {
                System.out.println("The valid currency codes are:");
                for (int idx = 0; idx < MAX_CURRENCIES; ++idx) {
                        System.out.print(CURRENCY_CODES[idx] + ",");
                }

                System.out.println();
//System.out.println(Arrays.toString(CURRENCY_CODES));
        }

        /**
         * Look up the given currency here and set the return value to true if found or
         * false if not found
         *
         * Hint: Use a FOR loop to traverse the loop
         *
         * @param myCurrencyToFind
         * @return
         */
        private static boolean lookUpCurrency(String myCurrencyToFind) {

                // For loop to iterate over CURRENCY_CODES array
                for (int i = 0; i < MAX_CURRENCIES; i++) {
                        // If the currency entered by user is equal to the currency at index i, break
                        // the loop
                        if (CURRENCY_CODES[i].equals(myCurrencyToFind)) {
                                System.out.printf("%s was found at index %d\n", CURRENCY_CODES[i], i);
                                return true;
                        }
                }

                return false;
        }

        /**
         * Prompt for a currency to find Call the lookUpCurrency with the inputted
         * currency code Display the found result Prompt to try again and exit this
         * method only if the choice is to not continue
         */

        private static void findMyCurrency() {
                // Scanner object to read input
                Scanner s = new Scanner(System.in);
                // Choice variable to store the user choice
                char choice = 'Y';
                // Until the user choice is -1, this loop is iterated
                while (choice != 'N') {
                        // Prompting the user to enter the currency
                        System.out.println("Enter the currency needed to be searched");
                        // Reading the input
                        String userCurrency = s.nextLine();
                        // Calling the method lookUpCurrency
                        boolean currencyFound = lookUpCurrency(userCurrency);
                        if (!currencyFound) {
                                System.out.println("Currency Code not found");
                        }

                        // Prompting the user to enter the choice
                        System.out.println("Retry Y/N ?");
                        // Reading the user choice
                        choice = s.nextLine().charAt(0);
                }
        }

        public static void main(String[] args) {
                // Loading the currency codes
                loadCurrencyCodes();
// Showing the currency codes to the user
                showCurrencies();
// Finding the currency entered by the user in the currency codes array
                findMyCurrency();
                System.out.println("The program has been terminated");

        }
}

It works fine.. I made some small changes so that it adheres to the instructions.. Please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
I am almost done with this code, but for some reason I am still getting an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I have almost done with this code to Implement Huffman Coding. However I have an error...

    I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine();    int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code");    Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Hello, can someone please fix my code? It runs perfect but I'm having trouble with the...

    Hello, can someone please fix my code? It runs perfect but I'm having trouble with the index for the incorrect answer array; please run some tests to ensure the code runs properly, thank you! Here is the code: ___________________________________________________________________________________________________________________________ import java.util.Scanner; public class DriverTest{    final static int QuestionTotal = 10;    public static void main(String[] args){          // scanner and answer key        Scanner scan = new Scanner(System.in);        final char[] answers = {'A', 'B', 'C',...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • Need to code the HeapSort Algorithm in java. Most of the code is already done just...

    Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol         if(l < k && String.Compare(pq[l], pq[n]) < 0)                           ^ symbol:   method Compare(String,String) location: class String Please fix the code and the "heapify"...

  • The files provided contain syntax and/or logic errors. In each case, determine and fix the problem,...

    The files provided contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. import java.util.*; public class DebugEight1 { public static void main(String args[]) { Scanner input = new Scanner(System.in); char userCode; String entry, message; boolean found = false; char[] okayCodes = {'A''C''T''H'}; StringBuffer prompt = new StringBuffer("Enter shipping code for this delivery\nValid codes are: "); for(int x = 0; x <...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT