• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays and For Loops.  RSS feed

 
ashlan cooper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there. I writing a program which uses arrays and for loops. In this program, it will ask the user to input their names and whether they are under 16. The data they entered will be stored in two arrays, adults and children.
In the end it will print an invoice listing names with the amount and also the total.

For examples;
what is the name of person 1? Beck
Is Beck under 16 ? No
what is the name of person 2? Jon
Is Jon under 16 ? Yes
.
.
.
.
.
What I have written so far



However I'm not sure how to put the names of adults and children to two different arrays.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should consider making a separate class for a person, where you can set all the information you want, including name and age.

Consider using a List of persons, rather than an array. They are designed for this sort of thing.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

This is a common situation in which your logic isn't clear, and you've written too much code without compiling. So now have lots of things that aren't working.

I recommend starting over with a new approach, characterized by 2 things:
  • Before writing any code, carefully describe your process (algorithm) in English. This will clarify your thought patterns, and should allow you to write more concise code.
  • When you do start writing code, write only a few lines at a time, then compile. If there are errors, fix them before moving on. This way, you will know exactly what works and what doesn't before you've accumulated dozens of lines with multiple problems.


  • As Stephan suggested, using a List (like an ArrayList) would be better suited to this problem. Do you need to use arrays?
     
    ashlan cooper
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank very much Marc. I would keep that in mind. I guess, I was rushing to finish the program. As for a beginner in programming I guess that would be a bad idea.
    However, I was just wondering I could I make 2 separate arrays for Children and Adults in another methods. I'm not very clear with ArrayList

    this is what I have modified
    though I can run, it stops after I type in an input for the very first question.

    could you please comment on it.

     
    Campbell Ritchie
    Marshal
    Posts: 56527
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marc has given you some good advice, which you don't appear to have taken. Your code is still difficult to understand.

    The following points will all lose you marks:
  • You have two methods which don't appear to do what their names suggest. In fact, they do nothing.
  • You are creating two ArrayLists, which you never use; in fact they go out of scope and can be garbage collected without any operation being done on them.
  • You have inconsistent variable naming (numberofparty, adults_names, which are both not correct style for Java).
  • Your indentation is inconsistent, sometimes using space and sometimes tab.
  • You will need to go back to what I keep telling our undergraduates to use: pencil paper and eraser. This is another version of what Marc told you. Write down exactly what you want each bit to do, and all the values. Write down the values of people.length and total, for example. Repeatedly simplify the procedure into smaller and smaller steps, then you will see what code to write.
    Have you been told to use JOptionPane, rather than Scanner, for input? That is very old-fashioned style.
    You haven't followed Stephan van Hulst's suggestion of injecting a little bit of object-orientation into your coding.
    I am glad to see you have got rid of the && true, which has no effect whatsoever.
     
    Jason Koonce
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As a beginning Java student, I definitely agree with Marc and Campbell. Step by step, compile as you go, and write it out first. One other thing I do is I declare my variables as I need them.

    For instance, I write my comment code ( // variable declarations ) but then no variables. As I go through my code, and need a new variable, I type it, then add it to my the line under my variable declaration comment. I do this because as I design my class I find that I would spend so much time and effort trying to figure out what variables I would need, that I actually lose sight of what I need the methods to do! So, I do it backwards. I design my method and add the variables I need when I need them. This way I only have the variables I need.

    I am not sure that would be considered a 'best-practice' but it works really well for me.
     
    Campbell Ritchie
    Marshal
    Posts: 56527
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I like the idea of adding variables as needed; too many beginners write variables (or even blocks of code) which they never need or never use.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7969
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. Variables are an implementation detail. Like like to write code top-down, from an interface/contract point of view.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!