Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: General Question

  1. #1
    Junior Member Becca's Avatar
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default General Question

    I'm not versed in technical java terms because I am transitioning from a different language so I will phrase my question sort of generically...

    I have 489 unique names (ASCII) each with an associated ID number. I can store this data set any way I choose. My input is a single string which I need to correlate to one of the names in my data set to retrieve the associated ID number.

    My two questions are:
    1. How should I store the data set?
    2. What is the best way to search the set and retrieve the correct ID number?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: General Question

    Take a look at the Map interface
    The Map Interface (The Java™ Tutorials > Collections > Interfaces)
    It will allow you to map keys to values. In the example you gave, there are many different ways to do this. For example, you could write a class which encapsulates the name and id. Then place these into a Map valued to the name or id (or both in different Maps)

  3. #3
    Junior Member Becca's Avatar
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General Question

    That seems like a good starting point. Thank you so much.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General Question

    Hi,
    If you are using Set as collection directly you can use contain method of set to test if that object present in that collection.

    bean factory
    Last edited by abani; December 18th, 2011 at 02:08 AM.

  5. #5
    Junior Member Becca's Avatar
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General Question

    Can someone tell me if the following actually does what I'm trying to do? This is my first time coding in Java. o_0


    public class SymbolCodeManager {

    String asciiSymbolPNG = "";

    public SymbolCodeManager(String asciiSymbol) {
    asciiSymbolPNG = asciiSymbol;
    }

    public int getSymbolCodeID () {
    Map<String, Integer> m = new HashMap<String, Integer>();
    try {
    Scanner s = new Scanner(new File("/home/rebecca/Desktop/SymbolCodeMap.txt"));
    while (s.hasNext()) {
    m.put(s.next(), s.nextInt());
    }
    } catch (IOException e) {
    System.err.println(e);
    System.exit(1);
    }

    if (m.containsKey(asciiSymbolPNG)){
    return m.get(asciiSymbolPNG);
    } else return 0;
    }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: General Question

    Can someone tell me if the following actually does what I'm trying to do?
    Quite possibly the best way to test this is to write a small little program that confirms your suspicions that it works or fails (in larger application development this is called writing a Test Case). You could do this by checking the values of the file are contained within the Map, iterating over the keys of the Map to verify it contains all the keys and their associated values, etc...this is an important programming concept which is why I recommend doing it this way. That being said, a more appropriate way to organize the code is to read the file once, then provide a method to retrieve its information (here, you read the file for every key you wish to check)

  7. #7
    Junior Member Becca's Avatar
    Join Date
    Nov 2011
    Posts
    8
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General Question

    Yes, you are absolutely right about moving the file read. Just the sort of insight I was hoping for. I do need to write a test case. Should be a good way to help me find my way around this new IDE. I miss my VC6 so much. /whining

Similar Threads

  1. Just a general Java related question
    By javaisfun in forum Java Theory & Questions
    Replies: 3
    Last Post: October 25th, 2011, 07:21 AM
  2. Newbie: general question on trying to get a simple Pokemon game to work
    By willmer in forum Object Oriented Programming
    Replies: 7
    Last Post: July 13th, 2011, 07:33 AM
  3. New to Java and programming in general
    By derekxec in forum Member Introductions
    Replies: 2
    Last Post: May 31st, 2011, 06:21 AM
  4. General CS concepts
    By helloworld922 in forum Java Programming Tutorials
    Replies: 14
    Last Post: April 21st, 2011, 10:35 AM
  5. General CS concepts
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 14
    Last Post: April 21st, 2011, 10:35 AM