Home > Back-end >  Can't get rid of spaces in input
Can't get rid of spaces in input

Time:04-16

I'm trying to code a states and capitals quiz and I almost have it complete however when I try to input any of the capitals where I have to put in 2 words (i.e. Little Rock or Oklahoma City) it has it as 2 separate inputs and for the life of me I can't get it to count it as one.

Here's my code thus far:

public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    
    ArrayList<String> States = new ArrayList<>();
    ArrayList<Object> Capitals = new ArrayList<>(States);
    
    
    String capitals [][] = {
        {"Alabama", "Montgomery"},
        {"Alaska", "Juneau"},
        {"Arizona", "Phoenix"},
        {"Arkansas", "LittleRock"},
        {"California", "Sacramento"},
        {"Colorado", "Denver"},
        {"Connecticut", "Hartford"},
        {"Delaware", "Dover"},
        {"Florida", "Tallahasse"},
        {"Georgia", "Atlanta"},
        {"Hawaii", "Honolulu"},
        {"Idaho", "Boise"},
        {"Illinois", "Springfield"},
        {"Indiana", "Indianapolis"},
        {"Iowa", "DesMoines"},
        {"Kansas", "Topeka"},
        {"Kentucky", "Frankfort"},
        {"Louisiana", "BatonRouge"},
        {"Maine", "Augusta"},
        {"Maryland", "Annapolis"},
        {"Massachusettes", "Boston"},
        {"Michigan", "Lansing"},
        {"Minnesota", "Saint Paul"},
        {"Mississippi", "Jackson"},
        {"Missouri", "JeffersonCity"},
        {"Montana", "Helena"},
        {"Nebraska", "Lincoln"},
        {"Nevada", "CarsonCity"},
        {"New Hampshire", "Concord"},
        {"New Jersey", "Trenton"},
        {"New York", "Albany"},
        {"New Mexico", "SantaFe"},
        {"North Carolina", "Raleigh"},
        {"North Dakota", "Bismark"},
        {"Ohio", "Columbus"},
        {"Oklahoma", "OklahomaCity"},
        {"Oregon", "Salem"},
        {"Pennslyvania", "Harrisburg"},
        {"Rhode Island", "Providence"},
        {"South Carolina", "Columbia"},
        {"South Dakota", "Pierre"},
        {"Tennessee", "Nashville"},
        {"Texas", "Austin"},
        {"Utah", "Salt Lake City"},
        {"Vermont", "Montpelier"},
        {"Virginia", "Richmond"},
        {"Washington", "Olympia"},
        {"West Virginia", "Charleston"},
        {"Wisconsin", "Madison"},
        {"Wyoming", "Cheyenne"}
    };
    
    
    for(int i = 0; i<=49; i  ){
        States.add(capitals[i][0]);
        Capitals.add(capitals[i][1]);
    }
    
    
    for(int i = 0; i<49; i  ){
        String guess;
        int index = (int)((Math.random())*(49 - i));
        System.out.println("What is the capital of: "   States.get(index));
        States.remove(index);
        
        guess = input.next();
        
        if(Capitals.get(index).equals(guess)){
            System.out.println("Correct");
            Capitals.remove(index);
        }else{ System.out.println("Incorrect");
            Capitals.remove(index);
        }
    }
    
    
}

CodePudding user response:

To elaborate on what @Gus is saying here " You could either change the delimiter pattern, or maybe use nextLine()",

Reading single tokens may be your downfall. "I like pie" is read separated as "I" "like" "pie" with line.next() but together as "I like pie" with line.nextLine()

EDIT FOR MORE DETAIL To bring an example line in:

for(int i = 0; i<=49; i  )    // {"Utah", "Salt Lake City"},
{
    States.add(capitals[i][0]); // add Utah
    Capitals.add(capitals[i][1]); // add Salt
}

In this index, capitals[44][], the size is set to 4 because of every single word read.

  • Related