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

Thread: Need help with some random java code.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with some random java code.

    Hello,

    I recently started learning java, and I am trying to make a program and I am running into some trouble. Basically what I want is for the user to input the number of people attending a movie, and then the user could input all of the ages of the people attending the movie, and then the program would print the total cost depending on the ages entered (child, senior, regular adult etc...).

    Here is what I have so far:

    import java.util.*;
     
    public class movietickets1 {
     
    	public static void main(String[] args) {
    		int numberpeople, answer;
     
    		Scanner xx=new Scanner(System.in);
    		System.out.println("Please enter the number of people attending the movie below:");
    		numberpeople=xx.nextInt();
     
    		int[] attendees = new int[numberpeople];
    		for (int i = 0; i < attendees.length; i++) {
     
     
    			System.out.print("Please enter the age of person number ");
    			System.out.print(i+1);
    			System.out.print(" below:");
    			attendees[i]=xx.nextInt();
     
     
    		}
     
     
     
    	}
     
    }

    Basically what I was thinking of doing was creating 3 more methods, that would return a variable that counted the number of people that were 12 and under for children, the number of people attending 65 and over for senior, and everything else in between for regular adults. Now I am not sure where to begin for what I want to accomplish for these 3 methods, as I am not sure how to get a method to return a variable that counted the number of items in an array that were above or below a certain value.

    Thanks!
    Last edited by helloworld922; October 5th, 2011 at 01:50 AM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Need help with some random java code.

    i think you should use buffred reader instead of scanner! try it

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with some random java code.

    Quote Originally Posted by arvindbis View Post
    i think you should use buffred reader instead of scanner! try it
    I don't think, if you read his/her problem??? Recommended to read first and stay on topic to provide better help.

    @shootingrubber: You must have a look at THIS!!!
    And always close the code in
     
    tags...(Forum Rules)

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Need help with some random java code.

    there is no problem with your code till yet! go and add more to it and if you got any problem do ask here!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with some random java code.

    Quote Originally Posted by arvindbis View Post
    there is no problem with your code till yet! go and add more to it and if you got any problem do ask here!
    Stop trolling....

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Need help with some random java code.

    Quote Originally Posted by Mr.777 View Post
    Stop trolling....
    okay, trolling was not my intension, i just tried to help him, infact encourage him/her but forget it!!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with some random java code.

    How could I return the number of values in an array greater than or less than a certain number? Like let's say the user entered 2 people who were 16 years of age or less, how could I get that method to return 2?

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with some random java code.

    If you read the link, i specified, you will come to know that a function can never return more than one value.
    Well, you can do it accoding to your problem description.
    You can either;
    1. Make a global array and put values in it.
    2. Pass the array into the function and assign values.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  3. [SOLVED] Java- Non repeating random questions.
    By arkaneraven in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2011, 09:55 AM
  4. java Logic Random with out repetition
    By Jhovarie in forum Loops & Control Statements
    Replies: 1
    Last Post: January 13th, 2011, 03:25 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM