Results 1 to 1 of 1
- 02-13-2012, 10:35 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
File to Array Writing and Searching Arrays
This is actually a 2-part question. Below is my code so far and I'm trying to read two files (girlnames.txt, and boynames.txt) and store the information in those files into arrays. The information in the files are both in this format: "Name 20123" without the parentheses.
My first issue is that I get an InputMismatchException on line 19 which is the line:
girlRegNumber[i] = input.nextInt();
I'm trying to store the name in the first array and the number following it in the array that the error resides in.
The above is my biggest issue right now, but if possible I need help at the end of my program where I am trying to search the two arrays that I'm making for a name and then eventually I will write more code to do something with the name and number but I can handle that.
Thanks for anyone who can help me and please let me know if I wasn't clear enough about what I'm trying to accomplish here.
Java Code:import java.util.*; import java.io.*; public class PopularNameSearch { public static void main (String [] args) { Scanner scn = new Scanner(System.in); String[] girlNames = new String[999]; String[] boyNames = new String[999]; int[] girlRegNumber = new int[999]; int[] boyRegNumber = new int[999]; try { Scanner input = new Scanner(new FileInputStream("girlnames.txt")); while (input.hasNext()) { for(int i=0;i<999;i++) { girlNames[i] = input.next(); } for(int i=0;i<999;i++) { girlRegNumber[i] = input.nextInt(); } } Scanner input2 = new Scanner(new FileInputStream("boynames.txt")); while (input2.hasNext()) { for(int i=0;i<999;i++) { boyNames[i] = input2.next(); } for(int i=0;i<999;i++) { boyRegNumber[i] = input2.nextInt(); } } } catch(Exception fnfe) { fnfe.printStackTrace(); } boolean fin = true; while (fin) { System.out.println("Please enter a name to search the database or 1 to quit the program:"); String name = scn.nextLine(); if (name=="1") { fin = false; System.exit(0); } else { for (String g : girlNames) { if (g.contains(name)) { System.out.println(g); } } for (String b : boyNames) { if (b.contains(name)) { System.out.println(b); } } } } } }
Similar Threads
-
Reading data into array from file and perform the searching
By konayuki1510 in forum New To JavaReplies: 11Last Post: 03-02-2011, 04:33 AM -
writing string array in to a file
By abdullahansari in forum New To JavaReplies: 13Last Post: 08-29-2010, 04:25 AM -
Writing integer pixel array(Range:0-255) into .txt file
By Mazharul in forum Java 2DReplies: 3Last Post: 08-24-2008, 01:51 PM -
Writing a countdown array to a file.
By kewlgeye in forum New To JavaReplies: 6Last Post: 05-25-2008, 06:09 AM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks