Results 1 to 5 of 5
  1. #1
    noahwhygodwhy is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Exclamation BPA registration simulation

    I'm taking programming in school and we were assigned a group project, this is only part of it, to simulate a BPA registration. I've been working on it for a while but i came upon this error and I just can't figure out what's wrong. It's a runtime so here is the error:

    Are you a:
    [1] Teacher
    [2] Student
    [3] Chaperone
    2
    How many general competitions are you going to participate in? (0-6)
    3
    1 Business Knowledge Skills
    2 Business Math
    3 Proofreading/Editing
    4 Business Law
    5 Economic Awareness
    6 Parliamentary procedure
    Which general competition would you like to participate in?
    Only one number at a time please, and then press enter,
    and then the next number, or if your done, type 'n' and press enter.
    1
    2
    3
    Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:417)
    at java.lang.Integer.parseInt(Integer.java:499)
    at GroupBPA.main(GroupBPA.java:90)
    Press any key to continue...

    And here is the program. I know part of it is missing but i really want this error solved before i continue. A quick response would be appreciated.

    import java.io.*;
    import java.util.Scanner;
    public class GroupBPA
    {
    public static void main(String[] args) throws IOException
    {
    String line = "";
    Scanner kb = new Scanner(System.in);
    System.out.println("Are you a:");
    System.out.println("[1] Teacher");
    System.out.println("[2] Student");
    System.out.println("[3] Chaperone");
    int person = kb.nextInt();
    if(person == 2)
    {
    //Scanner inS = new Scanner(new File("H:\\Programing\\Text Files\\BPA.txt"));//For School
    Scanner inS = new Scanner(new File("C:\\Users\\Noah\\BPA.txt"));//For Home
    String[] genCompetitions = new String[6];
    String[] TeamCompetitions = new String[4];
    String[] SPCompetitions = new String[31];
    int[] genCompetitionNum = new int[6];
    int[] TeamCompetitionNum = new int[4];
    int[] SPCompetitionNum = new int[31];
    for(int i = 1; i < 7; i++)
    {
    line = inS.nextLine();
    String[] s = line.split("\\s+");
    genCompetitionNum[i - 1] = (Integer.parseInt(s[0]));
    if(s.length == 2)
    genCompetitions[i - 1] = s[1];
    if(s.length == 3)
    genCompetitions[i - 1] = s[1] + " " + s[2];
    if(s.length == 4)
    genCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3];
    if(s.length == 5)
    genCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3] + " " + s[4];
    }
    for(int i = 1; i < 5; i++)
    {
    line = inS.nextLine();
    String[] s = line.split("\\s+");
    TeamCompetitionNum[i- 1] = (Integer.parseInt(s[0]) - 6);
    if(s.length == 2)
    TeamCompetitions[i - 1] = s[1];
    if(s.length == 3)
    TeamCompetitions[i - 1] = s[1] + " " + s[2];
    if(s.length == 4)
    TeamCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3];
    if(s.length == 5)
    TeamCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3]+ " " + s[4];
    }
    for(int i = 1; i < 32; i++)
    {
    line = inS.nextLine();
    String[] s = line.split("\\s+");
    SPCompetitionNum[i - 1] = (Integer.parseInt(s[0]) - 10);
    if(s.length == 2)
    SPCompetitions[i - 1] = s[1];
    if(s.length == 3)
    SPCompetitions[i - 1] = s[1] + " " + s[2];
    if(s.length == 4)
    SPCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3];
    if(s.length == 5)
    SPCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3] + " " + s[4];
    }
    boolean moreGen = false;
    System.out.println("How many general competitions are you going to participate in? (0-6)");
    int gens = kb.nextInt();
    for(int i = 0; i < 6; i++)
    {
    System.out.print(genCompetitionNum[i] + " ");
    System.out.println(genCompetitions[i]);
    }
    int i = 0;
    String[] genC = new String[6];
    System.out.println("Which general competition would you like to participate in?");
    System.out.println("Only one number at a time please, and then press enter,");
    System.out.println("and then the next number, or if your done, type 'n' and press enter.");
    do{
    genC[i] = kb.next();
    if(genC[i].equals("n"))
    {
    moreGen = true;
    }
    i++;
    }while(!moreGen && i < gens);
    String[] genComps = new String[genC.length];
    for(i = 0; i < genC.length; i++)
    {
    genComps[i] = genCompetitions[Integer.parseInt(genC[i])];
    }
    for(i = 1; i < 32; i++)
    {
    System.out.print(SPCompetitionNum[i - 1] + " ");
    System.out.println(SPCompetitions[i - 1]);
    }
    double sff = 0;
    double lunch = 0;
    double rwa= 0;
    double rwb = 0;
    System.out.println("Which one, if any, of the special competitions, listed above");
    System.out.println("are you going to participate in? Please type the number.");
    System.out.println("If you are not going to take part in one, type 'n' and press enter.");
    String SPChoice = kb.next();
    boolean SPCompete = false;
    String SPCompetition = "N/A";
    if(SPChoice.equals("n"))
    {
    System.out.println("Would you like to go to Sixflags Fiesta? 'y' or 'n'");
    String sffC = kb.next();
    if(sffC.equals("y"))
    {
    sff = 25;
    }
    System.out.println("You can also go to the luncheon, would you like to? 'y' or 'n'");
    String lunchC = kb.next();
    if(lunchC.equals("y"))
    {
    lunch = 10;
    }
    System.out.println("How about the first river walk? 'y' or 'n'");
    String rwaC = kb.next();
    if(rwaC.equals("y"))
    {
    rwa = 10;
    }
    System.out.println("how about the second? You know you want to. 'y' or 'n'");
    String rwbC = kb.next();
    if(rwbC.equals("y"))
    {
    rwb = 10;
    }
    SPCompete = false;
    }
    else
    {
    SPCompete = true;
    SPCompetition = SPCompetitions[Integer.parseInt(SPChoice) - 1];
    }
    if(Integer.parseInt(SPChoice) < 18)
    {
    System.out.println("Would you like to go to Sixflags Fiesta? 'y' or 'n'");
    String sffC = kb.next();
    if(sffC.equals("y"))
    {
    sff = 25;
    }
    System.out.println("You can also go to the luncheon, would you like to? 'y' or 'n'");
    String lunchC = kb.next();
    if(lunchC.equals("y"))
    {
    lunch = 10;
    }
    }
    if(Integer.parseInt(SPChoice) >17)
    {
    System.out.println("Would you like to go to Sixflags Fiesta? 'y' or 'n'");
    String sffC = kb.next();
    if(sffC.equals("y"))
    {
    sff = 25;
    }
    }
    if( lunch== 0)
    {
    String lunch2 = Double.toString(lunch);
    lunch2 = "N/A";
    }
    if(sff == 0)
    {
    String sff2 = Double.toString(sff);
    sff2 = "N/A";
    }
    if(rwb == 0)
    {
    String rwb2 = Double.toString(rwb);
    rwb2 = "N/A";
    }
    if(rwa == 0)
    {
    String rwa2 = Double.toString(rwa);
    rwa2 = "N/A";
    }
    }
    }
    }

    Just what am i doing wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your Scanner is probably not handling the end of line character adequately. Consider changing every kb.next() to kb.nextLine(). Consider adding after every kb.nextInt() a call to kb.nextLine() to swallow the end of line character.

    So
    Java Code:
    int foo kb.next();

    becomes

    Java Code:
    int foo kb.next();
    kb.nextLine();  // add this

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Your problem is that you create a String array with a length of 6. Object arrays have a default value of null. If you only insert 3 Strings into your array the other 3 elements will still be null. You then loop over the entire array and convert each element into an integer. As soon as it hits the null in the 4th position you get your exception.

    Also, BLECH!
    Java Code:
    if(s.length == 2)
    genCompetitions[i - 1] = s[1];
    if(s.length == 3)
    genCompetitions[i - 1] = s[1] + " " + s[2];
    if(s.length == 4)
    genCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3];
    if(s.length == 5)
    genCompetitions[i - 1] = s[1] + " " + s[2] + " " + s[3] + " " + s[4];
    Tidy this up (and all the other like snippets) with a loop.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    noahwhygodwhy is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Head Slap

    :eek: thanks for the quick response, i kinda feel stupid that i didn't see that, but i am new to java, thanks again.

Similar Threads

  1. JSP Registration Form error
    By ak88 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 10-26-2009, 10:54 AM
  2. help in registration
    By katrinax_caren02 in forum JDBC
    Replies: 1
    Last Post: 09-27-2009, 03:46 PM
  3. Duplicate entry in registration form!!!
    By anki1234 in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 08:15 PM
  4. Registration Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:22 AM

Tags for this Thread

Posting Permissions

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