I typed in this code on to GeeksforGeeks JAVA Ide and it says that I have a runtime error of Exception in thread "main" java.util.NoSuchElementException: No line found at...

I typed in this code on to GeeksforGeeks JAVA Ide and it says that I have a runtime error of Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at DynamicArray.main(DynamicArray.java:17) How do i fix this import java.util.*; public class DynamicArray { public static void main(String[] args) { String[] names = null; int[] frequencies = null; String name = ""; Scanner scanner = new Scanner(System.in); // loop until user hits enter key while (true) { System.out.print("Enter name (hit enter to stop): "); name = scanner.nextLine(); if (name.trim().length() == 0) break; // when the user enter the name for the first time if (names == null) { names = new String[1]; // create an array of size 1 frequencies = new int[1]; // create an array of size 1 names[0] = name; //store the current name frequencies[0] = 1; // store the count as 1 at the same index } else { // boolean nameExist = false; // go through the names to check for a match for (int i = 0; i if (names[i].equals(name)) { // if matach found frequencies[i] += 1; // just increment the value at the same index by 1 nameExist = true; break; } } // if the name dont exist in the array if (!nameExist) { // create a new array of size 1 plus the exisiting array size String[] tempNames = new String[names.length + 1]; int[] tempFrequencies = new int[frequencies.length + 1]; // copy all the element from the old array to the new array for (int i = 0; i tempNames[i] = names[i]; tempFrequencies[i] = frequencies[i]; } // add the new name and frequency as 1 tempNames[names.length] = name; tempFrequencies[names.length] = 1; // assign back the new array to the old array reference names = tempNames; frequencies = tempFrequencies; } } } // display the array contents for (int i = 0; i System.out.println(names[i] + " : " + frequencies[i]); } } }
May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here