#Able to display name from array but couldn't search it

16 messages · Page 1 of 1 (latest)

spare isle
#
Part of the code in main()

 while (choice != 3) {
           System.out.println("\n1. Enter name");
           System.out.println("2. Display all data from coins.txt (test)");
           System.out.println("3. Exit");
           System.out.print("Choice: ");
           choice = inputScanner.nextInt();
           inputScanner.nextLine();

            if (choice == 1) {
                System.out.print("Name: ");
                String targetName = inputScanner.nextLine();

                boolean found = false;
                
                // iterate through coin array to find matching name ignoring case
                 for (Coin coin : coins) {
                    if (coin != null && coin.getName().equalsIgnoreCase(targetName)) {        
                        Coin.CoinsChange(coin);
                        
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    System.out.println("\nName: " + targetName);
                    System.out.println("Not found");
                    
                    
                }
            }  else if (choice == 2) {
            displayAllData();
        }
            
            else if (choice == 3) {
                writeChangeToFile(coins, numCoins);
            }
        }
    }
robust stagBOT
#

This post has been reserved for your question.

Hey @spare isle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

spare isle
#
//Coin.java

public class Coin {
    private String name;
    private int cents;
    private String currency;
    
    public Coin() {
      
    }

    public Coin(String name, int cents, String currency) {
        this.name = name;
        this.cents = cents;
        this.currency = currency;
    }
    
    public String getName(){
        return name;
    }
    
    public int getCents(){
        return cents;
    }
    
    public String getCurrency(){
        return currency;
    }
    
    public static void CoinsChange(Coin coin) {
        int totalChange = coin.cents;
        int[] coinsCount = new int[4];

        int remainingChange = totalChange;

        while (remainingChange > 0) {
            if (remainingChange >= 50) {
                coinsCount[0]++;
                remainingChange -= 50;
            } else if (remainingChange >= 20) {
                coinsCount[1]++;
                remainingChange -= 20;
            } else if (remainingChange >= 10) {
                coinsCount[2]++;
                remainingChange -= 10;
            } else {
                coinsCount[3]++;
                remainingChange -= 1;
            }
        }

        System.out.println("\nCustomer:");
        System.out.println(coin.name + " " + totalChange + " cents in " + coin.currency);
        displayChange(coinsCount);
    }
    
    public static void displayChange(int[] coinsCount) {
        System.out.println("Change:");

        for (int i = 0; i < 4; i++) {
            if (coinsCount[i] > 0) {
                int denomination = (i == 3 ? 1 : (i == 2 ? 10 : (i == 1 ? 20 : 50)));
                System.out.println(denomination + " cents: " + coinsCount[i]);
            }
        }
    }
}
north gust
#

debugging will probably be the easiest way to find out the issue

spare isle
#

i found the problem i displayed using read file directly from my txt

#

my array didn't get any values from reading the txt at all

#

do you know how to make a scanner to get data from a txt file?

#

[name] [coin] "cents in" [currency]

#
  try (Scanner scanner = new Scanner(new File("coins.txt"))) {
            while (scanner.hasNextLine() && numCoins < 10) {
                String line = scanner.nextLine();
                String[] parts = line.split(" ");
                if (parts.length == 4) {
                    String name = parts[0];
                    int cents = Integer.parseInt(parts[1]);
                    String currency = parts[3];
                    if (cents >= 1 && cents <= 99) {
                        coins[numCoins] = new Coin(name, cents, currency);
                        numCoins++;
                    } if (cents > 99){
                        System.out.println("Incorrect value: " + line);
                    }
                } 
            }
        } catch (FileNotFoundException e) {
            System.out.println("Unable to open file.");
            return;
        }
north gust
#

the length will be 5, not 4

#

you never do anything when the length is 5

#

if there's a constraint on the file input, you could just assume that it'll be 5 and not even check

spare isle
#

omg thanks! i have been stuck on this for quite long

robust stagBOT
spare isle
#

im new to java 😆