Hello, so I am writing a program and had to create a one dimensional array and a two dimensional array. Here is my arrays:
privateGame [] games; private double[][]hoursArray = {{40, 60, 900, 400},{10,100, 800, 900},{20,70,100, 200}}; public GameSales() { games = new Game[ 3 ]; games[ 0 ] = new Game ("Scary Halloween","Mr. Boo",1000,6091,12.99); games[ 2 ] = new EGame ("Haunted House Party","Patrick Armstrong", 0, 2012,38.99,"www.SmartBook.com/IIS",180); games[ 3 ] = new Game ("The Green Monster", "Game Boy", 800, 3033,6.50);
so there is my arrays and constructer. Next I have to make another method. here are the instructions. I have tries a linear search but dont know if this is a good idea. I am very confused and am looking for any help. maybe a steer in the right direction
In this method, you will search for a specified game title. You will prompt user to enter the title of a game in string format, and search game array for the game. If the specified game is found in the game array, return the array index . Otherwise, return an integer value of 99 to indicate that the game was not on the list. You will use the result returned from this method in other method.
**You should implement a try-catch to catch a potential exception that might occur when dealing with iterating through an array. What would that be?
This is what i did, im not sure to use a linear search or index of
privatepublic String FindGame() { int searchString; int position; try { System.out.print ("Please enter game title: "); searchString = input.nextInt(); while (searchString != -1) { for (int index = 0; index < Game.length; index ++) if ( Game[ index ] == searchString ) return index; } } catch(InputMismatchException inputMismatchException) { System.err.printf ("\nExceptions: %s\n", inputMismatchException); input.nextLine(); System.out.println ("You must enter a String. Please try again. \n"); } return searchString; }