Home > Back-end >  Printing out a specific value after keyset
Printing out a specific value after keyset

Time:01-09

public static void main(String[] args) {
    HashMap<String, Integer>Car = new HashMap();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of employees worked with you this week");
    int Number = sc.nextInt();
    while (Number < 2 || Number > 5) {
        System.out.println("Enter a number between 2 and 5");
        Number = sc.nextInt();
    }
    System.out.println("Enter their names");
    String [] Name = new String [Number];
    sc.nextLine();
    for (int i=0; i<Number; i  ) {
        int a = i 1;
        System.out.println("Enter name of employee "   a);
        Name[i] = sc.nextLine();
    }
    int[] Cars = new int[Number];
    for (int i=0; i<Number; i  ) {
        String b = Name[i];
        System.out.println("Enter the number of cars sold by the employee "   b);
        Cars[i] = sc.nextInt();
        Car.put(Name[i], Cars[i]);
    }
    for (String i: Car.keySet()) {
        
    }
    ArrayList<Integer>CarOnly = new ArrayList<>(Car.values());
    Collections.sort(CarOnly, Collections.reverseOrder());
    for (int i: CarOnly) {
        for (String j: Car.keySet()) {
            if (i == Car.get(j))
                System.out.println(j);
        }
    }
    

}

}

I made this code to display the largest value with its String, however, instead of printing out a specific value, it prints out the entire value that is in the HashMap

CodePudding user response:

I suppose this is what you're trying to achieve ?

public class DumbCode {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final HashMap<String, Integer> hashMap = new HashMap<>();

        System.out.println("Enter the number of employees working with you this week:");
        int numberOfEmployees = scanner.nextInt();
        while(numberOfEmployees<2 || numberOfEmployees>5) {
            numberOfEmployees = scanner.nextInt();
        }

        int i=0;
        while(i  <numberOfEmployees) {
            System.out.println("Enter the name:");
            final String name = scanner.next();
            System.out.println("Enter the number of cars sold");
            final int carsSold = scanner.nextInt();
            hashMap.put(name, carsSold);
        }

        hashMap.entrySet().stream()
                .sorted((o1, o2) -> o1.getValue() > o2.getValue() ? 1 : 0)
                .limit(1)
                .forEach(o -> {
                    System.out.println(o.getKey()   " sold "  o.getValue() " cars.");
                });
    }
}

However I will also leave some advice for future, when asking questions, be clear in what your goal is. Also try to write cleaner code, For example, there are so many issues when trying to read your code.

  1. Variables names should not start with a capital letter unless you're declaring a constant.
  2. int Number = sc.nextInt(); does not convey the intention of the variable. int numberOfEmployees is much better name.
  3. for (String i: Car.keySet()) { } What is the use of putting this for loop if it's doing nothing ?
  4. If you want to store things in a HashMap, then you can take inputs of Key and Value at the same time and put them in directly instead of using 3 loops just to fill up a HashMap.
  5. Try using Functional programming over imperative programming since it's much more readable and concise.

CodePudding user response:

What I understood from your question is that you want to display the Name of the Employee who sold the most cars at the end of the program. If that's the case see the code below, I have replaced some of your code to get the job done, see comments in the code to find what part of the code is replaced:

public static void main(String[] args) {
HashMap<String, Integer>Car = new HashMap();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of employees worked with you this week");
int Number = sc.nextInt();
while (Number < 2 || Number > 5) {
    System.out.println("Enter a number between 2 and 5");
    Number = sc.nextInt();
}
System.out.println("Enter their names");
String [] Name = new String [Number];
sc.nextLine();
for (int i=0; i<Number; i  ) {
    int a = i 1;
    System.out.println("Enter name of employee "   a);
    Name[i] = sc.nextLine();
}
int[] Cars = new int[Number];
for (int i=0; i<Number; i  ) {
    String b = Name[i];
    System.out.println("Enter the number of cars sold by the employee "   b);
    Cars[i] = sc.nextInt();
    Car.put(Name[i], Cars[i]);
}
for (String i: Car.keySet()) {
    
}
ArrayList<Integer>CarOnly = new ArrayList<>(Car.values());
Collections.sort(CarOnly, Collections.reverseOrder());


// i have replaced the code that is using nested loops with the code below
int maxCars = 0;
String maxEmployee = "";
for (String name : Car.keySet()) {
    int numCars = Car.get(name);
    if (numCars > maxCars) {
        maxCars = numCars;
        maxEmployee = name;
    }
}
System.out.println("The employee who sold the most cars is "   maxEmployee   " with "   maxCars   " cars sold.");

}

Hope it Helps and please do consider reading this How do I ask a good question?

  • Related