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

    Default Program 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:

    Java Code:
    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!
    Last edited by JosAH; 05-10-2013 at 07:57 AM. Reason: added [code] ... [/code] tags

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

    Default Re: Program Code Help

    And sorry, I just realized my tabbing is off...i can repost it for anyone who can help me out with the correct tabbing!

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Program Code Help

    wrap it in code tags just remove the spaces from below

    [code ]

    PASTE CODE HERE

    [ / code]

  4. #4
    boeschej is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Program Code Help

    Sorry, I tried to apply the code tags and it didn't seem to work...

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

    Default Re: Program Code Help

    Quote Originally Posted by boeschej View Post
    Sorry, I tried to apply the code tags and it didn't seem to work...
    I did it for you; go on with the show ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Program Code Help

    heres a control loop solution:

    Java Code:
     public static void controlLoop() {
            while (true) {
             String x = Scanner.nextLine();
         if(x.equalsIgnoreCase("n")) {
    
          //do something
    
          }
    
        else if(x.equalsIgnoreCase("z")){
     
                        }
        }
    That's the syntax and logic, well its one way of doing it :) adapt that and place it in your own code

  7. #7
    boeschej is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Program Code Help

    Quote Originally Posted by monkeyjr97 View Post
    heres a control loop solution:

    Java Code:
     public static void controlLoop() {
            while (true) {
             String x = Scanner.nextLine();
         if(x.equalsIgnoreCase("n")) {
    
          //do something
    
          }
    
        else if(x.equalsIgnoreCase("z")){
     
                        }
        }
    That's the syntax and logic, well its one way of doing it :) adapt that and place it in your own code

    I tried to adapt this, but I am having so much trouble...I do not understand this at all...any other ideas/suggestions?

  8. #8
    boeschej is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: Program Code Help

    Here is my updated version: Now I just need help with the final array portion....any help?

    Java Code:
    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) {
                //Ask the user to enter an option (n, r, q)
                Scanner s = new Scanner(System.in);
                System.out.print("Search by (n)ame, (f)ile, or (q)uit? ");
                char input = (s.nextLine()).charAt(0);
                //If the user enters n
                    //Get the name from the user and call searchName
                if(input == 'n' || input == 'N'){
                    System.out.print("Enter name: ");
                    String name =(s.nextLine());
                    Proj7.searchName(name);
                }
                //If the user enters r
                    //Get the rank from the user
                if(input == 'r' || input =='R'){
                    System.out.print("Enter a ranking(1-1000");
                    int rank = Integer.parseInt(s.nextLine());
                    //Check that the rank is in the 1-1000 range
                    if(rank <= 1000 || rank >= 1){
                        Proj7.searchRanking(rank);
                    }
                    //If so, call searchRanking
                }
                //If the user enters q
                if(input == 'q' || input == '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) {
            //Loop through the names array with a for loop (use i to count)
            for(int i = 0; i< names.length; i++){
                if(names[i] == name){
                    int index = i;
                    return i;
                }
            }//If the current name in the array equals the name parameter, return
            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) {
    
            //Call nameIndex to get the index of name in your names array
            int index = Proj7.nameIndex(name);
                if (index == -1){
                    System.out.print("Error");   
                }
            //If the index is -1, print an error
            //Otherwise
                //get the rank at that index in the ranking array
                for(int i = 0; i<names.length; i++){
                    if(index == i){
                        String line = s.nextLine();
                        String[] pieces = line.split(" ");
                        names[i] = pieces[0];
                        rankings[i] = Integer.parseInt(pieces[1]);
                        System.out.print(names[i] + " is ranked " + rankings[i]);
                    }
                    else if(rankings[i] == 0){
                        System.out.print(names[i] + " is not ranked in the top 1000");
                    }
                }
                //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
        }
    }
    Last edited by JosAH; 05-11-2013 at 07:27 AM. Reason: add [code] ... [/code] tags again

  9. #9
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Program Code Help

    I'm not 100% sure what your commenting means but its hard to follow UN-formatted code.

    But i think its saying: pass a rank to search for in as parameters, then search your name array for this ranking. The name array will be searched suing 2 numbers, stored in the indicies array.

    my understanding maybe off but here it goes:

    Java Code:
    for(i = 0; i < indices.length; i++) { // start at 0 because arrays start indexing from 0
         int x = indices[i];    // x = position in indices array, in this case x = indices[0] then x = indices[1]
        System.out.print(names[x]); // print out the name in the names array based on values of x
    }
    This will get the result from your ranking method which will be stored in the indices array, it will do a loop which will execute x amount of times depending on the indices length. Then the values in the indices array will be retrieved and assigned to a variable and the name in the names array at said index will be printed.

    Not sure if im understood what you mean exactly, try that and tell me if it gives you desires results
    Last edited by monkeyjr97; 05-11-2013 at 05:01 AM.

Similar Threads

  1. Program won't run need help with code
    By Upcoming in forum New To Java
    Replies: 6
    Last Post: 04-28-2012, 06:56 AM
  2. error code on my program
    By andnlou2678 in forum New To Java
    Replies: 11
    Last Post: 10-11-2011, 11:26 PM
  3. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  4. What code would you put in a wedding program?
    By frenchzebu in forum Forum Lobby
    Replies: 1
    Last Post: 07-03-2010, 03:08 PM
  5. Replies: 2
    Last Post: 11-18-2008, 10: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
  •