java question

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1110 Views - Last Post: 19 October 2013 - 10:32 PM Rate Topic: -----

#1 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

java question

Posted 06 October 2013 - 07:59 AM

You are in a place where there are people of different ages. Write a program that will interview all the people in this place and count how many people exist of the following different group:
a. People in their twenties
b. People in their thirties
c. People in their forties
d. People younger than all the above categories
e. People older than all the above categories
Each time you add a new person to a category, you should print out how many persons already exist in this category after adding the new person.


would you help me doing it : )

Is This A Good Question/Topic? 0
  • +

Replies To: java question

#2 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: java question

Posted 06 October 2013 - 08:12 AM

What have you come up with so far? :) You should try it yourself first, and if you run into any problems i'll be more than happy to help you...but I'm not just going to give you the solution :) The best thing about problem solving through code, is proving that you can do it to yourself! It's far more rewarding that way...
Was This Post Helpful? 2
  • +
  • -

#3 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 08:26 AM

Thanks for your help :sweatdrop: :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#4 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 08:41 AM

import java.util.Scanner;


public class Age {

	public static void main(String[] args) {
		
		Scanner input= new Scanner(System.in);
		
		System.out.println("How many people are there:");
		int number=input.nextInt();
		
		for(int i=1;i<=number;i++)
		{
			if(number<30 && number>=20)
				System.out.println("people in their twenties");
				
			else
				if(number<40 && number>=30)
					System.out.println("people in their thirties");
			
				else
					if(number<20 && number>0)
						System.out.println("People younger than all the above categories");
			
					else
						if(number>40)
							System.out.println("People older than all the above categories");
				number++;
			
		}
		

	}

}


mod edit:
:code:


it will become an infinite loop :online2long:
how can i fix it :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#5 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: java question

Posted 06 October 2013 - 08:59 AM

Ok, so what you are doing currently is asking the user to tell you how many people there are...is this correct? I would assume what you want to do, is initially ask the user how many people there are, and then enter an age for each of those people...What your code is doing is basically this: It initially asks the user for the number of people, and then it uses that integer value (the one that the user gives) to run the for loop that you have. Unfortunately, at the end of the for loop, you increment the value that defines your for loop (number) every time the for loop enters a new cycle. This means that the for loop with continue indefinitely :D What you want to do is remove the number++ at the end of the loop...

However, for the purpose of your code, you also need to add another line of code SOMEWHERE (I say somewhere because i know you can figure it out yourself :) ) so that you are able to take in the individual ages of each of the people!

Have you been introduced to arrays yet?
Was This Post Helpful? 2
  • +
  • -

#6 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 09:10 AM

[quote name='manofax' date='06 October 2013 - 08:59 AM' timestamp='1381075140' post='1913368']
Ok, so what you are doing currently is asking the user to tell you how many people there are...is this correct? I would assume what you want to do, is initially ask the user how many people there are, and then enter an age for each of those people...What your code is doing is basically this: It initially asks the user for the number of people, and then it uses that integer value (the one that the user gives) to run the for loop that you have. Unfortunately, at the end of the for loop, you increment the value that defines your for loop (number) every time the for loop enters a new cycle. This means that the for loop with continue indefinitely :D/> What you want to do is remove the number++ at the end of the loop...

However, for the purpose of your code, you also need to add another line of code SOMEWHERE (I say somewhere because i know you can figure it out yourself :)/> ) so that you are able to take in the individual ages of each of the people!

Have you been introduced to arrays yet?
no not yet :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#7 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: java question

Posted 06 October 2013 - 09:12 AM

Ok, that means we cannot use them to solve your problem :D

Have you understood what i have explained so far? If not let me know what you are struggling with...That way at least i can give you some direct answers...
Was This Post Helpful? 2
  • +
  • -

#8 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 09:16 AM

[quote name='manofax' date='06 October 2013 - 09:12 AM' timestamp='1381075974' post='1913373']
Ok, that means we cannot use them to solve your problem :D/>

Have you understood what i have explained so far? If not let me know what you are struggling with...That way at least i can give you some direct answers...
how can i enter an age for each of those people and add a new person Each time to a category :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#9 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: java question

Posted 06 October 2013 - 09:26 AM

Perhaps think about it like this.
you want to perform a certain block of code x times (with x the input you receive, ie the number of people).
Their is something that can simulate this behaviour.
Was This Post Helpful? 1
  • +
  • -

#10 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: java question

Posted 06 October 2013 - 09:27 AM

Currently you have a loop that runs as many times are there are people (entered by your user and stored in the integer value called number).

You need to now have integer values representing each category, as well as an integer value for the age entered. For example:

int numTwenties; numThirties, numForties, numLess, numGreater, age;


Then, at the beginning of your loop you need to make a call to your Scanner to get a new value from the user (the age).

System.out.println("Enter the person's age");
age = input.nextInt(); 


This allows you to then get an age for EACH person that the loop is handling. You then need to change your if statements so that it will test the age entered instead of the number of users...

The if statement should then increment the category it is testing against, which will then allow you to print out the total amount of people that each category contains at the end of the loop!

Do you understand the logic behind this?
Was This Post Helpful? 2
  • +
  • -

#11 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 09:34 AM

[quote name='manofax' date='06 October 2013 - 09:27 AM' timestamp='1381076879' post='1913378']
Currently you have a loop that runs as many times are there are people (entered by your user and stored in the integer value called number).

You need to now have integer values representing each category, as well as an integer value for the age entered. For example:

int numTwenties; numThirties, numForties, numLess, numGreater, age;


Then, at the beginning of your loop you need to make a call to your Scanner to get a new value from the user (the age).

System.out.println("Enter the person's age");
age = input.nextInt(); 


This allows you to then get an age for EACH person that the loop is handling. You then need to change your if statements so that it will test the age entered instead of the number of users...

The if statement should then increment the category it is testing against, which will then allow you to print out the total amount of people that each category contains at the end of the loop!

Do you understand the logic behind this?
yes
but
when i run the program:
How many people are there:
3
Enter the person's age
3 22 45
People younger than all the above categories
People younger than all the above categories
People younger than all the above categories

their is a problem in for loop statment maybe
Was This Post Helpful? 0
  • +
  • -

#12 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: java question

Posted 06 October 2013 - 09:35 AM

please post your new code so we can see what the problem is.
Was This Post Helpful? 0
  • +
  • -

#13 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 09:41 AM

View Postaxel1994, on 06 October 2013 - 09:35 AM, said:

please post your new code so we can see what the problem is.

import java.util.Scanner;


public class Age {

public static void main(String[] args) {

Scanner input= new Scanner(System.in);

System.out.println("How many people are there:");
int number=input.nextInt();



int numTwenties, numThirties, numForties, numLess, numGreater, age;

System.out.println("Enter the person's age:");
age=input.nextInt();

while()
for(int i=1;i<=age;i++)
{
if(age<30 && age>=20)
System.out.println("people in their twenties");

else
if(age<40 && age>=30)
System.out.println("people in their thirties");

else
if(age<20 && age>0)
System.out.println("People younger than all the above categories");

else
if(age>40)
System.out.println("People older than all the above categories");


}

}

}
Was This Post Helpful? 0
  • +
  • -

#14 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: java question

Posted 06 October 2013 - 09:59 AM

For future reference, it makes your code a lot easier if you put it in the predefined code format, which can be used by inserting your code between the "
" and "
" bracers.

I would suggest rewriting your code in a more manageable fashion, its easier to determine problems when you can see them in a clearer manner. For example, instead of writing your loops like this:

if (criterion)

else
if(criterion)


try doing it in a more manageable way, for example:

if (criterion){

}else if(criterion){

}else{

}


Bracers make it a lot easier to see where your comparisons start and end...
Was This Post Helpful? 1
  • +
  • -

#15 dark_chocolate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 13

Re: java question

Posted 06 October 2013 - 10:06 AM

import java.util.Scanner;


public class Age {

	public static void main(String[] args) {
		
		Scanner input= new Scanner(System.in);
		
		System.out.println("How many people are there:");
		int number=input.nextInt();
		


int numTwenties, numThirties, numForties, numLess, numGreater, age;

		System.out.println("Enter the person's age:");
		age=input.nextInt();
		
		while()
		for(int i=1;i<=age;i++)
		{
			if(age<30 && age>=20)
				System.out.println("people in their twenties");
				
			else
				if(age<40 && age>=30)
					System.out.println("people in their thirties");
			
				else
					if(age<20 && age>0)
						System.out.println("People younger than all the above categories");
			
					else
						if(age>40)
							System.out.println("People older than all the above categories");
				
			
		}
		
			}

}

i put the braces :rolleyes:

This post has been edited by andrewsw: 06 October 2013 - 10:11 AM
Reason for edit:: Use code tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2