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 4 of 4

Thread: How to store 2 related elements using a 2x2 array

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default How to store 2 related elements using a 2x2 array

    Please guys,

    I have the simple table below:

    currency amount
    € 2.0
    $ 4.0
    £ 5.0

    How could I store the currency and amount in an array? A 2x2 array would do this but how to store them and retrieve them
    is the challenge. For example, I have a method that asks the user for two inputs, the currency and the amount and using the array as a chat table where I could map the currency to the the currency input entered by the user, I could do some calculations with the amount entered by the user. Anyone, willing to help or point me to how I could represent the 2x2 array?

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to store 2 related elements using a 2x2 array

    Why would you need a 2D array for this?

    There are several options: have two arrays (not a 2D array), one that holds the currency and another that holds the amounts. Match the indexes of both arrays up. Or you could use a Map<currency, amount>. Or you could use an Object that held a currency and amount, then just create whatever instances of that Object you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    help_desk (July 16th, 2014)

  4. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: How to store 2 related elements using a 2x2 array

    Hi KevinWorman,

    Ok, let's assume the table above is now stored in a file..
    //file.txt

    3 //this is the length of each line.
    € 2.0
    $ 4.0
    £ 5.0

    This would mean, the file has 3 lines with each line containing the currency and the value; both separated by a space..Now, I want to read each line and get only the currency and store in a single array.. The code below is what I have written except I need a way to modify it to work with what I am thinking..

    String currencyArray[] = new String[6]; //declare the array
    BufferedReader br;
    		try {
    			br = new BufferedReader(new FileReader(new File("file.txt")));
    			String line = new String();
                            int i = 0;
    			while ((line = br.readLine()) != null) {
    				String[] lineParts = line.split("\\s+");
    				  currencyArray[i] = lineParts[0];
                                      i++;
    			}

    I just want to know if my idea is correct? Also, the first line in the file has the number 3 which is the number of lines containing the currency-amount pair. The conventional thing to do is to read the first line and use that to reserve memory for the array == new int[3]; and then go to the nextLine() and start reading the values into the currencyArray array but bufferedReader do not have the nextLine() method. Anyway to get this around.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to store 2 related elements using a 2x2 array

    Quote Originally Posted by help_desk View Post
    I just want to know if my idea is correct?
    You tell us: does the code you posted work? Does it do what you expect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: October 31st, 2013, 05:31 AM
  2. [SOLVED] matching elements of one array with another array.
    By shenaz in forum Java Theory & Questions
    Replies: 13
    Last Post: March 29th, 2013, 10:31 AM
  3. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  4. [SOLVED] Array related assignment
    By Melawe in forum Java Theory & Questions
    Replies: 36
    Last Post: July 21st, 2010, 03:22 AM

Tags for this Thread