Results 1 to 2 of 2
  1. #1
    boeschej is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default In desperate need of code help

    Hello! So I am in a CIS class and we use Bluej, our assignment is to write a program that reads a file that ranks the popularity of names in the year 2000. You will then let the user get statistics by name and by ranking. We were provided a input file with the list of names and rank. I have started the program but I am having trouble finishing and compiling it. Can anyone help me with my missing parts? PLEASE PLEASE?! Here are the requirements for the program:
    Requirements
    This section contains more details on requirements of the project:
    • You should accept upper- or lower-case input for the command options (n, r, and q). If the
    user enters a different letter, you should print an error and prompt again for input.
    • You may assume that users will search for names using the same capitalization as in the
    names file (capital first letter and the rest lower-case, like “Robin”)
    • You should print an error if the user searches for a name that was not in the file
    • You should print an error if the user searches for a ranking that is not in the range 1-1000.
    • When searching by ranking, there will be TWO names with the given rank (a boy’s name and
    a girl’s name). You should print them both.
    • When searching by name, if the file had a “0” as the ranking, you should print that the name
    was not ranked in the top 1000. Do not print 0 as the ranking.
    • You should store the names and the rankings from the file in two arrays.

    Like I said, I just need help finishing it. This is what I have so far:


    import java.io.*;
    import java.util.*;

    public class Proj7 {
    private static String[] names;
    private static int[] rankings;
    private static Scanner s;

    public static void main(String[] args) throws IOException {

    s = new Scanner(System.in);
    System.out.print("Enter name of file: ");
    String filename = s.nextLine();

    readNamesData(filename);
    controlLoop();
    }

    /**
    * readNamesData reads a file of name statistics and stores the data
    *
    * @param filename The filename
    */
    public static void readNamesData(String filename) throws IOException {

    Scanner inFile = new Scanner(new File(filename));
    int size = Integer.parseInt(inFile.nextLine());
    inFile.nextLine();

    names = new String[size];
    rankings = new int[size];

    for (int i = 0; i < size; i++) {
    String line = inFile.nextLine();
    String[] pieces = line.split(" ");
    names[i] = pieces[0];
    rankings[i] = Integer.parseInt(pieces[1]);
    }

    inFile.close();
    }

    /**
    * controlLoop repeatedly lets the user search by name, ranking,
    * or quit the program
    */
    public static void controlLoop() {
    while (true) {
    //NEED HELP WITH THIS:
    //Ask the user to enter an option (n, r, q)
    //If the user enters n
    //Get the name from the user and call searchName

    //If the user enters r
    //Get the rank from the user
    //Check that the rank is in the 1-1000 range
    //If so, call searchRanking

    //If the user enters q
    return; //(this leaves the controlLoop) }
    }
    }

    /**
    * 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
    */
    public static int nameIndex(String name) {
    //NEED HELP WITH THIS:
    //Loop through the names array with a for loop (use i to count)
    //If the current name in the array equals the name parameter, return i



    return -1; //(-1 will be returned if the name isn't in the array)
    }

    /**
    * searchName prints the ranking for the given name.
    *
    * @param name The name to print statistics for
    */
    public static void searchName(String name) {
    //NEED HELP WITH THIS:

    //Call nameIndex to get the index of name in your names array
    //If the index is -1, print an error
    //Otherwise
    //get the rank at that index in the ranking array
    //If the rank is 0, this means the name is not ranked in the top 1000
    //Otherwise, print the ranking of the name
    }

    /**
    * rankIndex gets the two index of rank within the rankings array
    *
    * @param rank The rank to find
    * @return The array of the two indices of rank in the rankings array
    */
    public static int[] rankIndex(int rank) {

    int[] spots = new int[2];

    int pos = 0;
    for (int i = 0; i < rankings.length; i++) {
    if (rankings[i] == rank) {
    spots[pos] = i;
    pos++;
    }
    }

    return spots;
    }

    /**
    * searchRanking prints the names with the given ranking
    *
    * @param rank The rank to search for
    */
    public static void searchRanking(int rank) {
    int[] indices = rankIndex(rank);

    //NEED HELP WITH THIS:
    //indices is an array with two elements. The first position is the
    //index of the first name with that ranking, and the second position is the
    //index of the second name with that ranking.

    //Look up those two indices in your names array, and print the names
    }
    }


    If anyone can help fill in the blanks for me I will be forever grateful! and you will be the master of all that is JAVA in my mind!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: In desperate need of code help

    This is a duplicate thread; please don't double post; I'm closing the thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. I'm new and in desperate need of help
    By Bdufty in forum Introductions
    Replies: 4
    Last Post: 04-30-2012, 07:18 AM
  2. Desperate: How do I fix this error?
    By pondwire in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 09:25 AM
  3. Replies: 1
    Last Post: 01-20-2011, 05:59 PM
  4. help i'm desperate
    By AniMaind in forum New To Java
    Replies: 17
    Last Post: 01-08-2011, 02:05 AM
  5. Desperate for some help if possible :(
    By SBOSlayer in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •