简体   繁体   中英

Working with Java; HashMaps and ArrayLists not compiling in Eclipse or BlueJay

I am having trouble getting HashMaps and ArrayLists to work correctly on my computer. I have tried using my own code, and copying samples from textbooks and online to make sure my syntax is correct, but thus far neither Eclipse or BlueJay will let me "add" or "put" things into these data structures. Here are some examples of what I have done.

package issues;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;

public class StructureIssues {

/*
 * HashMap Attempt
 */
    HashMap<Integer, String> numberNames = new HashMap<Integer, String>();

    numberNames.put(new Integer(1), "hi");// here, I have syntax errors asking me to 
                                      // delete the (), and I have misplaced
                                      // constructs on the dot.

    //When the above line didn't work, I tried creating the objects
    //outside of the parameter list...
    Integer one = new Integer(1);
        String myString = "hi";
    numberNames.put(one, myString); //here it just complains about the parenthesis
                                    //similar results for <String,String> and generic


/*
 * ArrayList Attempt
 */
    ArrayList<String> tryStrings = new ArrayList<String>();
    String tryOne = "one";
    tryStrings.add(tryOne);//Syntax error on tryOne; variable declarator ID expected
                       //also, syntax error on the dot; misplaced constructs

    ArrayList<Integer> tryInts = new ArrayList<Integer>();
    tryInts.add(new Integer(4));//Syntax error on add; expected "=" after it.

    //Below, I have copied two lines from Big Java by Horstmann. 
//The results are the same as my first String ArrayList attempt.
    ArrayList<String> friends = new ArrayList<String>();
    friends.add("Cindy");

}

I did find one or two questions similar to mine, and I followed their advice, but so far I've had no luck. Here is what I have tried to far to figure this out:

-Reinstalled my JDK package several times, trying both 64 and 32-bit

-Reinstalled eclipse Indigo several times, trying both 64 and 32-bit

-In eclipse, gone to Project->Properties->Java Compiler. My java compliance is JavaSE-1.7

-In eclipse, gone to Window->Preferences->InstalledJREs. I have jre7 with Standard VM.

-I have tried right clicking on my jre System Library in my packages, and changing to JavaSE-1.6, 1.7, and checking the default workspace box for jre7.

-I have tried similar code in BlueJay because I wanted to try another IDE to see if it was eclipse or my computer. I received: "identifier" expected. It highlighted tryStrings.add("one");

Am I doing something silly here? Any advice you guys could offer would be greatly appreciated. Thank you for your time.

Your code is not in any method. You may declare and initialize fields in a class. But using these fields should be done in methods (or constructors).

Problem is that the code is not in any method. Where you are calling the put method is the area where you declare the variables. see this modified code. I made the variable static so that it can be called from the main method.

public class StructureIssues {

/*
 * HashMap Attempt
 */
static HashMap<Integer, String> numberNames = new HashMap<Integer, String>();

public static void main(String args[]) {
    numberNames.put(new Integer(1), "hi");// here, I have syntax errors
                                            // asking me to
                                            // delete the (), and I have
                                            // misplaced
                                            // constructs on the dot.

    // When the above line didn't work, I tried creating the objects
    // outside of the parameter list...
    Integer one = new Integer(1);
    String myString = "hi";
    numberNames.put(one, myString); // here it just complains about the
                                    // parenthesis
                                    // similar results for <String,String>
                                    // and generic

    /*
     * ArrayList Attempt
     */
    ArrayList<String> tryStrings = new ArrayList<String>();
    String tryOne = "one";
    tryStrings.add(tryOne);// Syntax error on tryOne; variable declarator ID
                            // expected
                            // also, syntax error on the dot; misplaced
                            // constructs

    ArrayList<Integer> tryInts = new ArrayList<Integer>();
    tryInts.add(new Integer(4));// Syntax error on add; expected "=" after
                                // it.

    // Below, I have copied two lines from Big Java by Horstmann.
    // The results are the same as my first String ArrayList attempt.
    ArrayList<String> friends = new ArrayList<String>();
    friends.add("Cindy");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM