-2

I keep getting this error and also can someone read the instructions at the bottom and can you tell me if I did this correctly? Especially the part 3. Your help will be greatly appreciated.

Can someone help me?

Exception in thread "main" java.lang.NumberFormatException: For input string: "Jacob" at

java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)

at java.lang.Integer.(Integer.java:867)

at assign14.Assign14.main(Assign14.java:49)

package assign14 {

import java.io.IOException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;

public class P62_akp7103 {

public static Map[] boys = new Map[10];
public static Map[] girls = new Map[10];

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.print("Enter a yeat in the range [2001, 2010]: ");
    String year = reader.next();
    System.out.print("Enter a gender [M/F]: ");
    String gender = reader.next();
    System.out.print("Enter a name: ");
    String name = reader.next();
    System.out.println();

    Map<String, String> mapBoys = new HashMap<>();
    Map<String, String> mapGirls = new HashMap<>();
    int rank = 0;

    try {
        java.net.URL url = new java.net.URL(
                "http://www.cs.armstrong.edu/liang/data/babynamesranking"
                + year + ".txt");

        // Create input file from url
        Scanner input = new Scanner(url.openStream());
        while (input.hasNext()) {
            String s = input.next();
            String[] temp = s.split(" ");
           //This is where the error is 
            if (gender.equalsIgnoreCase("M")) {
                rank = new Integer(temp[0]);
            } else if (temp[3].contains(name)) {
                rank = new Integer(temp[0]);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Invalid Year");
    }
    if (rank != 0) {
        System.out.println(name + " is ranked #" + rank + " in year " + year);

    }
}
}

1] Write a program that prompts the user to enter a year in the range [2001, 2010], a gender and a name, then displays the ranking of the name for the selected year and gender.

2] The data files consist of lines of data, each containing a ranking (an integer), a boy's name (a string), the number of boy's that were given that name (an integer), a girl's name (a string) and the number of girls that where given that name (an integer). Values are separated by spaces and/or tabs, which java.util.Scanner will use as separators by default.

3] The data is to be stored in two arrays of maps, one for boy’s names and one for girl’s names. Each array must have one element for each of the 10 years of data. Each element in the array is a map (a class from the Java API that implements interface Map) that stores key / value pairs, each pair consisting of a name and its ranking, with the name serving as the key.

This is what one of the txt file looks like

Number 1-10 is their rank

1 Jacob 30541 Emily 24450

2 Michael 28220 Madison 21771

3 Joshua 25965 Hannah 18802

4 Matthew 25142 Emma 16520

5 Ethan 22099 Alexis 15629

6 Andrew 21996 Ashley 15335

7 Joseph 21872 Abigail 15292

8 Christopher 21665 Sarah 14741

9 Nicholas 21380 Samantha 14652

10 Daniel 21291 Olivia 14627

This is what the sample output looks like

Enter a year: 2002
Enter a gender M/F: M
Enter a name: Jacob

Jacob name was ranked #1 in 2002
Community
  • 1
  • 1
John Casey
  • 45
  • 1
  • 7
  • How exactly are you entering your data? Do you actually put 5 space delimited values on one line and then press Enter? – PM 77-1 Oct 14 '17 at 16:16
  • Just edited my code. Look all the way at the bottom – John Casey Oct 14 '17 at 16:16
  • 1
    Possible duplicate of [What's the difference between next() and nextLine() methods from Scanner class?](https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class) – PM 77-1 Oct 14 '17 at 16:22

1 Answers1

0

java.lang.NumberFormatException error for an input from a user

You getting NumberFormatException String s = input.next(); and split it by String[] temp = s.split(" "); for each and every word at index 0 temp[0] every-time, When It's trying to parse name as String it gives you exception java.lang.NumberFormatExceptio at point here rank = new Integer(temp[0]); because you didn't break to next line at last by input.nextLine(); and Trying to parse each and every word in every line defined inside text file.

Below I'm posting the solution checkout & Try to understand your missing code part.

import java.io.IOException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class P62_akp7103 {

    public static Map[] boys = new Map[10];
    public static Map[] girls = new Map[10];

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        System.out.print("Enter a yeat in the range [2001, 2010]: ");
        String year = reader.next();
        System.out.print("Enter a gender [M/F]: ");
        String gender = reader.next();
        System.out.print("Enter a name: ");
        String name = reader.next();
        System.out.println();

        Map<String, String> mapBoys = new HashMap<>();
        Map<String, String> mapGirls = new HashMap<>();
        int rank = 0;

        try {
            java.net.URL url = new java.net.URL(
                    "http://www.cs.armstrong.edu/liang/data/babynamesranking"
                    + year + ".txt");

            // Create input file from url
            Scanner input = new Scanner(url.openStream());

                while (input.hasNext()) {
                    String token = input.next();
                    String[] temp = token.split(" ");
                    if (gender.equalsIgnoreCase("M")) {
                        Object ob = temp[0];
                        try {
                            rank = new Integer("" + ob.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if (temp[3].contains(name)) {
                        rank = new Integer(temp[0]);
                    }
                    input.nextLine();
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Invalid Year");
        }
        if (rank != 0) {
            System.out.println(name + " is ranked #" + rank + " in year " + year);

        }
    }
}