Need Urgent Help With Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thegreatest21
    New Member
    • Aug 2007
    • 6

    Need Urgent Help With Arrays

    Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to grip with Arrays...so I am here to ask for help and see what help I can get. Here's the code that I already have;

    Code:
    System.out.println("Please Enter In Your First Name: ");
    
                     String firstName = bufRead.readLine();
    Code:
    System.out.println("What is your household income per year?: ");
    
                     String houseIncome = bufRead.readLine();
    The values that are entered here need to be put into an ArrayList, which can then be revisited and called upon when requested by the user.

    Any help will be appreciated as I need to hand in soon!
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by thegreatest21
    Right, I am making a Tax Calculator and need an array to store the data that has been typed in when the user is prompted. However, being completely new to Java I am having some difficulty getting to grip with Arrays...so I am here to ask for help and see what help I can get. Here's the code that I already have;

    Code:
    System.out.println("Please Enter In Your First Name: ");
    
                     String firstName = bufRead.readLine();
    Code:
    System.out.println("What is your household income per year?: ");
    
                     String houseIncome = bufRead.readLine();
    The values that are entered here need to be put into an ArrayList, which can then be revisited and called upon when requested by the user.

    Any help will be appreciated as I need to hand in soon!
    Well ... I know that you have a problem... but I don't know what that problem is. I will ask a few things first though. One, do you want to use an array or an ArrayList, because in java they are a little bit different. The biggest difference being that you can continuously add to ArrayList while with Arrays you have to declare a certain size or number of values in that array.

    Take a look at the documentation for the ArrayList class: http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html

    Some things to note:
    To initialize an ArrayList you should type the following:
    Code:
    ArrayList<Object o> arraylist = new ArrayList<Object o>(initial capacity of ArrayList) or you can leave the parentheses blank with a ().
    For the object I assume you're using Strings, but here comes the other thing I want to ask, why are you storing all inputs as strings? Shouldn't household income be stored as a double? Also, I would suggest using the Scanner class instead of bufferReader, check out the documentation here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

    That way you could use readInt() or readDouble() not just readLine().

    Its all up to you. Good luck and we'll be here to help you with any problems you run into.

    -blazed

    Comment

    • thegreatest21
      New Member
      • Aug 2007
      • 6

      #3
      Originally posted by blazedaces
      Well ... I know that you have a problem... but I don't know what that problem is. I will ask a few things first though. One, do you want to use an array or an ArrayList, because in java they are a little bit different. The biggest difference being that you can continuously add to ArrayList while with Arrays you have to declare a certain size or number of values in that array.

      Take a look at the documentation for the ArrayList class: http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html

      Some things to note:
      To initialize an ArrayList you should type the following:
      Code:
      ArrayList<Object o> arraylist = new ArrayList<Object o>(initial capacity of ArrayList) or you can leave the parentheses blank with a ().
      For the object I assume you're using Strings, but here comes the other thing I want to ask, why are you storing all inputs as strings? Shouldn't household income be stored as a double? Also, I would suggest using the Scanner class instead of bufferReader, check out the documentation here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

      That way you could use readInt() or readDouble() not just readLine().

      Its all up to you. Good luck and we'll be here to help you with any problems you run into.

      -blazed
      I want to use an ArrayList and the other thing is householdIncome is as a double. I forgot to mention that sorry.

      Comment

      • blazedaces
        Contributor
        • May 2007
        • 284

        #4
        Originally posted by thegreatest21
        I want to use an ArrayList and the other thing is householdIncome is as a double. I forgot to mention that sorry.
        So... what exactly is the problem again? You have yet to tell us what's holding you back...

        -blazed

        Comment

        • thegreatest21
          New Member
          • Aug 2007
          • 6

          #5
          Sorry for this...Basicall y I want the data that is being entered to be stored in an ArrayList. For instance, if a name is entered here;

          Code:
          System.out.println("Please Enter In Your First Name: ");
          
                           String firstName = bufRead.readLine();
          Then I want to be displayed in an ArrayList but I currently am confused by how it is explained in tutorials. If you can show me how an ArrayList would look then maybe that would help.

          Also I need to repeatedly ask for details from successive users, would I be using a do while loop for that?

          Sorry for all this!

          thegreatest21

          Comment

          • blazedaces
            Contributor
            • May 2007
            • 284

            #6
            Originally posted by thegreatest21
            Sorry for this...Basicall y I want the data that is being entered to be stored in an ArrayList. For instance, if a name is entered here;

            Code:
            System.out.println("Please Enter In Your First Name: ");
            
                             String firstName = bufRead.readLine();
            Then I want to be displayed in an ArrayList but I currently am confused by how it is explained in tutorials. If you can show me how an ArrayList would look then maybe that would help.

            Also I need to repeatedly ask for details from successive users, would I be using a do while loop for that?

            Sorry for all this!

            thegreatest21
            I posted the documentation for things up there. I advise you to refer to them as much as possible, but I'll answer you questions as much as possible:

            If you were going to store all inputs as Strings (which I advise against) and you wanted it all stored in a single ArrayList (still don't understand the reason for this) you would write your code to be something like this:

            Code:
            Scanner input = new Scanner(System.in);
            ArrayList<String> list = new ArrayList<String>();
            
            System.out.println("Ask question for a string here: ");
            list.add(input.next()); //Or nextLine() depending on what you want, again, read the scanner class, believe me it's a better choice then bufferReader for this kind of keyboard input
            ArrayLists are simple, there's an add(Object o) method, an add(Object o, int index), a get method for those same things and a bunch of other convenient things like .size(), etc.

            As for whether you want to use a do-while loop... I don't know. In fact, I would say no, you don't, because you want to ask different questions. Still, it all depends on what you want, you could use a while loop, but I think you should just ask the questions one at a time... or write methods to ask the questions or a method to loop through them.

            You use a while loop where it's needed, remember it's simply while(boolean is true) keep looping. Just a tool you have at your disposal. Remember that the entire point of coding anything, methods, classes, loops, are all there for being lazy. Or more efficient, the way I would like to look at it. Just do whatever you're more comfortable with or would let you code less.

            Good luck,
            -blazed

            Comment

            • thegreatest21
              New Member
              • Aug 2007
              • 6

              #7
              Thank You Very Much BlazedAces! :D

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by thegreatest21
                Thank You Very Much BlazedAces! :D
                I strongly suggest you'd create a simple Household class:

                [code=java]
                publc class Household {
                private String name;
                private double income;
                // etc.
                }
                [/code]

                Read the constituents of a Household object, convert them to the correct type
                and create a new Household object from them and stick that in a List of some
                sort.

                kind regards,

                Jos

                Comment

                Working...