Results 1 to 8 of 8
Thread: Java Project
- 03-18-2011, 11:21 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Java Project
So I'm trying to make a program that will search user input from a read in txt file that has names as well as the decades and ranks for that name in that decade. Below the code is essentially what needs to be read in just cut down a whole bunch. Looking for a way to set it up so that that the years and names are set up in their own array as well as with the rankings in a 2d array not sure if im doing things right or not.
Java Code:/** * readNamesData reads a file of name statistics and stores the data * * @param filename The filename */ public static void readNamesData(String filename) throws IOException { //this method should read the input file (called filename) //store all information in the global variables (listed above main) Scanner inFile=new Scanner(new File(filename)); int size= Integer.parseInt(inFile.nextLine()); String list = inFile.nextLine(); String [] n = list.split(" "); rankings= new int [size][n.length]; while(inFile.hasNext()){ for(int x = 1; x < size; x++){ names[x]= names[0]; for( int i = 1; i <n.length-1; i ++){ years [i-1] = Integer.parseInt (names [i]); inFile.nextLine(); } } } inFile.close(); }4408
name 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
- 03-19-2011, 01:06 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If I'm correct, what you've is kind of a grid (cross tabulation) with the names and there ranks in each decade.
If so is anything wrong with the results you end-up with? Because your question is not clear enough to me.
- 03-19-2011, 01:13 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
This is the whole program that I'm working with, it is a type of grid like you said , the problem I'm having with it is taking the information from the txt and putting the information into its corresponding array/method so that the user can search for a name, letter or year and get the the information about any of those.
Java Code:import java.io.*; import java.util.*; public class Proj2 { //an array of all names in the file private static String[] names; //an array of all years in the file (for namesRanking.txt, decades from 1900-2000) private static int[] years; //rankings by name and year. The rows should be the names, and the columns should //be the years. So rankings [10][2] should be the ranking of the name at index 10 in names //during the year at index 2 in years private static int[][] rankings; //for command-line user input private static Scanner s; public static void main(String[] args) throws IOException { //create a new Scanner //get the input file from the user //call readNamesData to read the file //call controlLoop to start the main program loop s= new Scanner(System.in); System.out.print("Enter name of file:"); String fil = s.nextLine(); readNamesData(fil); controlLoop(); } /** * readNamesData reads a file of name statistics and stores the data * * @param filename The filename */ public static void readNamesData(String filename) throws IOException { //this method should read the input file (called filename) //store all information in the global variables (listed above main) Scanner inFile=new Scanner(new File(filename)); int size= Integer.parseInt(inFile.nextLine()); String list = inFile.nextLine(); String [] n = list.split(" "); rankings= new int [size][n.length]; while(inFile.hasNext()){ for(int x = 1; x < size; x++){ names[x]= names[0]; for( int i = 1; i <n.length-1; i ++){ years [i-1] = Integer.parseInt (names [i]); inFile.nextLine(); rankings[x][i]= Integer.parseInt(list.substring(i,i+1)); } } } inFile.close(); } /** * controlLoop repeatedly lets the user search by name, year, letter, * or quit the program */ public static void controlLoop() { //repeatedly ask the user to search by name, year, letter, //or if they want to quit s = new Scanner(System.in); char option; do{System.out.print("\nSearch <n>ame, <y>ear, <l>etter, or <q>uit:"); option = (s.nextLine()).charAt(0); switch (option) { case 'n': System.out.print("Enter name:"); String n = s.nextLine(); searchName(n); break; case 'l': System.out.print("Enter letter:"); char l = s.nextLine().charAt(0); System.out.print("How high did you want the \"top names\" list to go?"); int ll = Integer.parseInt(s.nextLine()); searchLetter(l,ll); break; case 'y': System.out.print("Enter year:"); int y = Integer.parseInt(s.nextLine()); System.out.print("How high did you want the \"top names\" list to go?"); int yy = Integer.parseInt(s.nextLine()); searchYear(y,yy); break; case 'q': break; } } while (option != 'q'); //for each option, call the appropriate method (searchName, etc.) //to execute that command } /** * nameIndex gets the index of name within the names array * * @param name The name to find * @return The index of name in the names array */ private static int nameIndex(String name) { //should return the index of name within the names array //Leave this here -- it means the name can't be found return -1; } /** * yearIndex gets the index of a year in the years array. * If year isn't a decade, it returns the index of the closest * decade (e.g. if year is 1982, it returns the index of 1980) * * @param year The year to find * @return The index of the closest decade to that year */ private static int yearIndex(int year) { //should return the index of year within the years array //if year is not a decade, return the index of the closest decade //Leave this year -- it means the year can't be found return -1; } /** * searchName prints the highest ranking (+year) and current ranking * for the given name. * * @param name The name to print statistics for */ public static void searchName(String name) { nameIndex(name); //should print the highest ranking name has ever had (plus the year) //and the current ranking name has (during the latest year, 2000) System.out.println(name + " had its highest ranking of "+"in"); System.out.println("In 2000, "+" was ranked "); } /** * searchYear prints the top names the decade closest to a given year * * @param year The year * @param max How high to rank the names (2 will be printed per ranking) */ public static void searchYear(int year, int max) { //should print the top names in year, with rankings from 1 up to max //You will find two names for each ranking in that year (one male and one female) -- //print them both. } /** * searchLetter prints the top names in the latest year that start * with a given letter * * @param letter The letter the names should start with * @param max How high to rank the names */ public static void searchLetter(char letter, int max) { //should print the top names in the latest year (2000), with //rankings from 1 up to max //Just print one name per ranking. If two names are tied for the //same ranking, you can order them however you want. //Be aware that your "1" ranking here will probably NOT be a name //with a "1" ranking. It will be the name in 2000 that has a higher //ranking than any other name beginning with that letter } }
- 03-19-2011, 01:21 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's not easy task to keep your result (grid as we named it) in a collection like ArrayList as it is. So you can think of a way to store records in multiples, decades in a one, names in a one, and a map to ranks in a one. And there are many other ways to keep them too. It depends on how user can search too. Is they can search all fields?
- 03-19-2011, 01:32 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
- 03-19-2011, 01:42 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If use has three options you can easily move with three ArrayList. But you have to careful of maintaining the mapping between all data.
- 03-19-2011, 02:33 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
yeah that's my problem I'm trying to get it so it read it from that the file and puts it into its set array but i keep messing up and need help with that part
- 03-23-2011, 08:28 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So what you've tried. Start with a simple example could more easy.
Similar Threads
-
Java Project
By larry_d1990 in forum New To JavaReplies: 2Last Post: 02-21-2011, 07:22 PM -
need help with java project
By doxycycline in forum New To JavaReplies: 11Last Post: 02-07-2011, 04:05 PM -
Java Project
By Smirre in forum New To JavaReplies: 16Last Post: 11-17-2008, 08:37 PM -
UML to Java project
By banie in forum NetBeansReplies: 3Last Post: 01-28-2008, 10:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks