Results 1 to 10 of 10
  1. #1
    minihazard10 is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default Never makes it to the method!

    Hey,

    I can't figure out why this program never makes it to the optionOne or optionTwo methods. It just prints the menu over and over. Any help would be greatly appreciated...


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

    public class FloridaRecount {
    public static final String FILE_NAME = "recount.txt";
    public static final int NUM_CODERS = 3;
    public static final int CERTIFIED_BUSH = 2912790;
    public static final int CERTIFIED_GORE = 2912253;

    public static final String[] COUNTIES = {"Broward", "Miami-Dade", "Palm Beach", "Volusia"};
    public static final boolean [] USED_PUNCH_BALLOTS = {true, true, true, false};

    public static void main(String[] args) throws FileNotFoundException{
    Scanner console = new Scanner(System.in);
    Scanner input = new Scanner(new File(FILE_NAME));
    input.useDelimiter("[\t\n]");
    menu(console);

    }

    public static int menu(Scanner console) throws FileNotFoundException{
    Scanner input = new Scanner(new File(FILE_NAME));
    input.useDelimiter("[\t\n]");
    int [] bushCounts = new int[0];
    int [] goreCounts = new int [0];
    System.out.println("Welcome to the Florida Recount Calculator");
    System.out.println();
    System.out.println("Possible standards for which ballot markings should be counted: ");
    System.out.println(" 1) only filled ovals and fully detached chads");
    System.out.println(" 2) any sign of intent (e.g. partically detached or dimpled chads)");
    System.out.print("Which standard do you want to use? ");
    int standard = console.nextInt();
    System.out.println();
    System.out.print("How many of the coders must agree (2-3)? ");
    int coders = console.nextInt();
    System.out.println();
    if (standard == 1){
    optionOne(console, bushCounts, goreCounts);
    }
    else{
    optionTwo(console, bushCounts, goreCounts);
    }
    return coders;
    }


    public static void optionOne(Scanner console, int [] bushCounts, int [] goreCounts) throws FileNotFoundException{
    Scanner input = new Scanner(new File(FILE_NAME));
    input.useDelimiter("[\t\n]");
    int codersNeeded = menu(console);
    while (input.hasNext()){
    String countyName = input.next();
    int county_num = getCountyNum(countyName);
    int numCodersBush = 0;
    int numCodersGore = 0;
    int i = 0;
    int j = 0;
    while (i <= NUM_CODERS && j <= COUNTIES.length - 1){
    int bushCode = input.nextInt();
    int goreCode = input.nextInt();
    if (USED_PUNCH_BALLOTS[0] || USED_PUNCH_BALLOTS[1] || USED_PUNCH_BALLOTS[2] ){
    if (bushCode == 4 && goreCode != 4){
    numCodersBush ++;
    }
    else if (goreCode == 4 && goreCode != 4){
    numCodersGore ++;
    }
    }
    if (USED_PUNCH_BALLOTS[3]){
    if (bushCode == 44 && goreCode != 44){
    numCodersBush ++;
    }
    if (goreCode == 44 && bushCode != 44){
    numCodersGore ++;
    }
    }
    i++;
    j++;
    }
    if (numCodersBush >= codersNeeded){
    if (county_num == 0){
    bushCounts[0]++;
    }
    else if (county_num == 1){
    bushCounts[1]++;
    }
    else if (county_num == 2){
    bushCounts[2]++;
    }
    else{
    bushCounts[3]++;
    }
    }
    else if (numCodersGore >= codersNeeded){
    if (county_num == 0){
    goreCounts[0]++;
    }
    else if (county_num == 1){
    goreCounts[1]++;
    }
    else if (county_num == 2){
    goreCounts[2]++;
    }
    else{
    goreCounts[3]++;
    }
    }
    }
    System.out.println("Votes gained by county:");
    System.out.println(" Bush Gore");
    printCounty(COUNTIES[0]);
    System.out.println(bushCounts[0] + goreCounts[0]);
    printCounty(COUNTIES[1]);
    System.out.println(bushCounts[1] + goreCounts[1]);
    printCounty(COUNTIES[2]);
    System.out.println(bushCounts[2] + goreCounts[2]);
    printCounty(COUNTIES[3]);
    System.out.println(bushCounts[3] + goreCounts[3]);
    System.out.println("Final results:");
    int bushFinal = CERTIFIED_BUSH + bushCounts[0] + bushCounts[1] + bushCounts [2] + bushCounts [3];
    int goreFinal = CERTIFIED_GORE + goreCounts[0] + goreCounts[1] + goreCounts [2] + goreCounts [3];
    System.out.println("Bush: " + bushFinal);
    System.out.println("Gore: " + goreFinal);
    if (bushFinal > goreFinal){
    System.out.println("Bush wins by " + (bushFinal - goreFinal) + "votes.");
    }
    else{
    System.out.println("Gore wins by " + (goreFinal - bushFinal) + "votes.");
    }
    }


    public static void optionTwo(Scanner console, int [] bushCounts, int [] goreCounts) throws FileNotFoundException{
    Scanner input = new Scanner(new File(FILE_NAME));
    input.useDelimiter("[\t\n]");
    int codersNeeded = menu(console);
    int numCodersBush = 0;
    int numCodersGore = 0;
    int i = 0;
    int j = 0;
    String countyName;
    int county_num = 0;
    while (input.hasNext()){
    countyName = input.next();
    county_num = getCountyNum(countyName);
    while (i <= NUM_CODERS && j <= COUNTIES.length - 1){
    int bushCode = input.nextInt();
    int goreCode = input.nextInt();
    if (USED_PUNCH_BALLOTS[0] || USED_PUNCH_BALLOTS[1] || USED_PUNCH_BALLOTS[2] ){
    if (bushCode > 0 && goreCode <= 0){
    numCodersBush ++;
    }
    else if (goreCode > 0 && goreCode <= 0){
    numCodersGore ++;
    }
    }
    if (USED_PUNCH_BALLOTS[3]){
    if (bushCode > 0 && bushCode != 99 && (goreCode <= 0 || goreCode == 99)){
    numCodersBush ++;
    }
    if (goreCode > 0 && goreCode != 99 && (bushCode <= 0 || goreCode == 99)){
    numCodersGore ++;
    }
    }
    i++;
    j++;
    }
    }
    if (numCodersBush >= codersNeeded){
    if (county_num == 0){
    bushCounts[0]++;
    }
    else if (county_num == 1){
    bushCounts[1]++;
    }
    else if (county_num == 2){
    bushCounts[2]++;
    }
    else{
    bushCounts[3]++;
    }
    }
    else if (numCodersGore >= codersNeeded){
    if (county_num == 0){
    goreCounts[0]++;
    }
    else if (county_num == 1){
    goreCounts[1]++;
    }
    else if (county_num == 2){
    goreCounts[2]++;
    }
    else{
    goreCounts[3]++;
    }
    }
    System.out.println("Votes gained by county:");
    System.out.println(" Bush Gore");
    printCounty(COUNTIES[0]);
    System.out.println(bushCounts[0] + goreCounts[0]);
    printCounty(COUNTIES[1]);
    System.out.println(bushCounts[1] + goreCounts[1]);
    printCounty(COUNTIES[2]);
    System.out.println(bushCounts[2] + goreCounts[2]);
    printCounty(COUNTIES[3]);
    System.out.println(bushCounts[3] + goreCounts[3]);
    System.out.println("Final results:");
    int bushFinal = CERTIFIED_BUSH + bushCounts[0] + bushCounts[1] + bushCounts [2] + bushCounts [3];
    int goreFinal = CERTIFIED_GORE + goreCounts[0] + goreCounts[1] + goreCounts [2] + goreCounts [3];
    System.out.println("Bush: " + bushFinal);
    System.out.println("Gore: " + goreFinal);
    if (bushFinal > goreFinal){
    System.out.println("Bush wins by " + (bushFinal - goreFinal) + "votes.");
    }
    else{
    System.out.println("Gore wins by " + (goreFinal - bushFinal) + "votes.");
    }
    }



    /*
    * printCounty - prints the name of a county with the correct
    * amount of padding so that the results will line up in columns.
    */

    public static void printCounty(String name) {
    System.out.printf("%-14.14s\t", name);
    }

    public static int getCountyNum(String countyName) {
    // Add code to return the index of the specified county
    // in the COUNTIES array.
    if (countyName.equals("Boward")){
    return 0;
    }
    if (countyName.equals("Miami-Dade")){
    return 1;
    }
    else if (countyName.equals("Palm Beach")){
    return 2;
    }
    else if (countyName.equals("Volusia")){
    return 3;
    }
    else{
    return -1;
    }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you specifically ask your question? What are the option you are talking about. I don't think anyone wants to run your code and test that what's going on there.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Try debugging your code by adding some println() statements to show variable values and execution flow.

    How do you know that neither of those methds are executed?

    Please use code tags to format your code. Unformatted code is very hard to read.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    It DOES make it to those methods, though.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    The OP doesn't think so. If he adds some println()s he will see where it executes.

  6. #6
    minihazard10 is offline Member
    Join Date
    Oct 2008
    Posts
    7
    Rep Power
    0

    Default

    I have figured out the problem I was trying to describe, sorry for not being clear!

    I was recursively calling menu in the option methods and the option methods in the menu.

    Anyway, now I have a runtime error:
    java.util.InputMismatchException

    I'm not entirely sure what this exception entails though...

    Thanks so much and sorry for the confusion!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    So why are you trying to read different/incompatible data type?

    Please use code tags next time you posting codes here and formatted it. Un-formatted codes are hard to read.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    java.util.InputMismatchException
    Read the API doc for that class. It explains why the exception is thrown.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. What makes a good programmer?
    By cruxblack in forum Forum Lobby
    Replies: 35
    Last Post: 11-16-2008, 04:22 AM
  3. how to use a variable of one method in another method
    By lucasautomacao in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 02:21 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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