Java: High and Low Value

High and Low Value

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2325 Views - Last Post: 13 April 2007 - 04:40 PM Rate Topic: ***** 1 Votes

#1 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Java: High and Low Value

Posted 09 April 2007 - 06:40 PM

/*
  This program should allow the user to enter the age in years and weight in pounds of an indefinite
  number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
  of -1 when you are done entering the information. Calculate the average weight and age for all
  the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
  screen the number of people on which information was entered. Print the highest age and the lowest
  age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/

import java.util.*;

import java.text.DecimalFormat;

/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()/*Method for data*/

	  {
			/*Variables*/

		  int people = 0;
		  double ageAverage = 0;
		  double weightAverage = 0;
		  int age;
		  int weight;
		  int ageTotal = 0;
		  int weightTotal = 0;



		Scanner scannerObject = new Scanner(System.in);

	/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");
		  System.out.println("and weight in pounds of an indefinite number of people.");
		  System.out.println("Enter the ages and weights as integer values.");
		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");
		  System.out.println("");
		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");
		  System.out.println("\n");



/** While statement to create loop for user. **/

 age = scannerObject.nextInt();

		  while (age!= -2) /*While loop to enter. Enter -1 to exit */


		   	{

			  ageTotal += age; /*Sums all the scores entered.*/


			  people ++; /* Counts the number of people entered */

			  System.out.println("Enter another age or -2 to enter weight.");

			 age = scannerObject.nextInt();

			  }/*End of while loop.*/
System.out.println("\n");
System.out.println("Enter weight or -1 to end");


		  ageAverage = ageTotal / people; /*Calculates average age.*/

weight = scannerObject.nextInt();
while (weight!= -1)
{
	weightTotal += weight;
	people ++;
	System.out.println("Enter another weight or -1 to end");
	weight = scannerObject.nextInt();
}
System.out.println("\n");

weightAverage = weightTotal / people;

		 DecimalFormat twoDigits = new DecimalFormat("0.00");


		/** Create new DecimalFormat object named twoDigits **/

	/*Displays number of people, total score and average score.*/

		  System.out.println("You entered " + people + " people.");
		  System.out.println("The sum of your age is  " + ageTotal);
		  System.out.println("The sum of your weight is " + weightTotal);
		  System.out.println("Your average age is  " + twoDigits.format(ageAverage));
		  System.out.println("Your average weight is  " + twoDigits.format(weightAverage));
		  System.out.println("\n\n");
Scanner scan = new Scanner (System.in);







	  }/*End of method for data.*/

	  public static void main(String[] args)

	  {/*Main method.*/

		  People.peopleData();

	  }/*End of main method.*/

  }/*End of class People*/


I don't know how to implement the highest value, lowest value for weight and age into the above so it calculates with my already compiled program. I need this to calculate the age and weight with my program above. Any help? Please.

Scanner scan = new Scanner (System.in);

int age = 0, lowest = 0, highest = 0;

for (int i = 0; i <; i++) {

   System.out.println("Enter a number");
	age  = scan.nextInt();

	if (i == 0) {
	  lowest = age;
	  highest = age;
	} // end if

	else if (age < lowest)
	   lowest = age;
	else if (age > highest)
	   highest = age; 


Is This A Good Question/Topic? 0
  • +

Replies To: Java: High and Low Value

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java: High and Low Value

Posted 10 April 2007 - 12:37 PM

HI, I don't think that this code will do:

Quote

Scanner scan = new Scanner (System.in);

int age = 0, lowest = 0, highest = 0;

for (int i = 0; i <; i++) {

   System.out.println("Enter a number");
	age  = scan.nextInt();

	if (i == 0) {
	  lowest = age;
	  highest = age;
	} // end if

	else if (age < lowest)
	   lowest = age;
	else if (age > highest)
	   highest = age;


You have a for loop that does nothing.
for(i = 0; i <people; i++)

the for loop does nothing, because nothing changes the value of the variables remains the same.

So I suggest that when the user enters the numbers instead of int age variable , you declare int age[]; an array. But then instead of a while loop you'll use a for loop
for (int i=0;age[i]!=-2;i++) to read the numbers of the user;
for(int i=0;a[i]!=-2;i++)/*For loop to enter. Enter -1 to exit */


			   {

			  ageTotal += age[i]; /*Sums all the scores entered.*/


			  people ++; /* Counts the number of people entered */
...
...
...





After that
for(i = 0; i <people; i++)
  if (i == 0) {
	  lowest = age[i];
	  highest = age[i];
	} // end if

	else if (age[i] < lowest)
	   lowest = age[i];
	else if (age[i] > highest)
	   highest = age[i];

This post has been edited by PennyBoki: 10 April 2007 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 10 April 2007 - 02:09 PM

View PostPennyBoki, on 10 Apr, 2007 - 12:37 PM, said:

HI, I don't think that this code will do:

Quote

Scanner scan = new Scanner (System.in);

int age = 0, lowest = 0, highest = 0;

for (int i = 0; i <; i++) {

   System.out.println("Enter a number");
	age  = scan.nextInt();

	if (i == 0) {
	  lowest = age;
	  highest = age;
	} // end if

	else if (age < lowest)
	   lowest = age;
	else if (age > highest)
	   highest = age;


You have a for loop that does nothing.
for(i = 0; i <people; i++)

the for loop does nothing, because nothing changes the value of the variables remains the same.

So I suggest that when the user enters the numbers instead of int age variable , you declare int age[]; an array. But then instead of a while loop you'll use a for loop
for (int i=0;age[i]!=-2;i++) to read the numbers of the user;
for(int i=0;a[i]!=-2;i++)/*For loop to enter. Enter -1 to exit */


			   {

			  ageTotal += age[i]; /*Sums all the scores entered.*/


			  people ++; /* Counts the number of people entered */
...
...
...





After that
for(i = 0; i <people; i++)
  if (i == 0) {
	  lowest = age[i];
	  highest = age[i];
	} // end if

	else if (age[i] < lowest)
	   lowest = age[i];
	else if (age[i] > highest)
	   highest = age[i];





So I am still having trouble trying to put this code in so that it works.

It does not compile properly with the technique that you gave me. I

need further help as to where to place the code. Lost, please help.

Thanks.

/*
  This program should allow the user to enter the age in years and weight in pounds of an indefinite
  number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
  of -1 when you are done entering the information. Calculate the average weight and age for all
  the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
  screen the number of people on which information was entered. Print the highest age and the lowest
  age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/

import java.util.*;

import java.text.DecimalFormat;

/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()/*Method for data*/

	  {
			/*Variables*/

		  int people = 0;
		  double ageAverage = 0;
		  double weightAverage = 0;
		  int age[];
		  int weight[];
		  int ageTotal = 0;
		  int weightTotal = 0;



		Scanner scannerObject = new Scanner(System.in);

	/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");
		  System.out.println("and weight in pounds of an indefinite number of people.");
		  System.out.println("Enter the ages and weights as integer values.");
		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");
		  System.out.println("");
		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");
		  System.out.println("\n");


  Scanner scan = new Scanner (System.in);





/** While statement to create loop for user. **/


{
	 ageTotal += age[i]; /*Sums all the scores entered.*/


			  people ++; /* Counts the number of people entered */
 age = scannerObject.nextInt[];






			  System.out.println("Enter another age or -2 to enter weight.");



			  }

System.out.println("\n");
System.out.println("Enter weight or -1 to end");


		  ageAverage = ageTotal / people; /*Calculates average age.*/

weight = scannerObject.nextInt[];





{
	weightTotal += weight[i];
	people ++;
	System.out.println("Enter another weight or -1 to end");
	weight = scannerObject.nextInt();
}
System.out.println("\n");

weightAverage = weightTotal / people;

		 DecimalFormat twoDigits = new DecimalFormat("0.00");


		/** Create new DecimalFormat object named twoDigits **/

	/*Displays number of people, total score and average score.*/

		  System.out.println("You entered " + people + " people.");
		  System.out.println("The sum of your age is  " + ageTotal);
		  System.out.println("The sum of your weight is " + weightTotal);
		  System.out.println("Your average age is  " + twoDigits.format(ageAverage));
		  System.out.println("Your average weight is  " + twoDigits.format(weightAverage));
		  System.out.println("\n\n");



	  }/*End of method for data.*/


 public static void main(String[] args)
	  {/*Main method.*/

		  People.peopleData();

	  }/*End of main method.*/

  }/*End of class People*/




/* Sir- Please look at these statements. It will not read these in my program. What have
I done that I can correct? Thanks*/



/*weight = scannerObject.nextInt();
	Scanner scan = new Scanner (System.in);

			int weight[] = 0, lowest = 0, highest = 0;

			for(int i=0;i<people;i++)
			if (i == 0) {
				lowest = weight[i];
				highest = weight[i];
			} //end if

			else if (weight[i] < lowest)
			lowest = weight[i];
			else if (weight[i] > highest)
			highest = weight[i];
for(int=0;weight[i]!=-1;i++)
{
*/


/*Scanner scan = new Scanner (System.in);
for(int i=0;age[i]!=-1;i++)
int age[] = 0, lowest = 0, highest = 0;

for(int i=0;i<people;i++)
if (i == 0) {
	lowest = age[i];
	highest = age[i];
} //end if

else if (age[i] < lowest)
lowest = age[i];
else if (age[i] > highest)
highest = age[i];  */


Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java: High and Low Value

Posted 10 April 2007 - 03:20 PM

hi again

Quote

/** While statement to create loop for user. **/


{
ageTotal += age[i]; /*Sums all the scores entered.*/


people ++; /* Counts the number of people entered */
age = scannerObject.nextInt[];


instead of the comment add
for(int i=0;a[i]!=-2;i++)

and change

Quote

age = scannerObject.nextInt[];

to
age[i] = scannerObject.nextInt[];

try it and think through it if u can't do it come again
Was This Post Helpful? 0
  • +
  • -

#5 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 10 April 2007 - 04:01 PM

Okay here is what I have corrected and I compiled it and it came back with 11 errors.

What am I doing wrong now?

Thanks.

/*
  This program should allow the user to enter the age in years and weight in pounds of an indefinite
  number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
  of -1 when you are done entering the information. Calculate the average weight and age for all
  the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
  screen the number of people on which information was entered. Print the highest age and the lowest
  age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/

import java.util.*;

import java.text.DecimalFormat;

/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()	 /*Method for data*/

	  {
			/*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;



		Scanner scannerObject = new Scanner(System.in);

			/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");

		  System.out.println("and weight in pounds of an indefinite number of people.");

		  System.out.println("Enter the ages and weights as integer values.");

		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");

		  System.out.println("");

		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");

		  System.out.println("\n");


  Scanner scan = new Scanner (System.in);

  for(int i = 0; age[i]! = -2; i++)

{
	ageTotal += age[i]; 	/*Sums all the ages entered.*/

	people ++; 	/* Counts the number of people entered */

	System.out.println("Enter another age or -2 to enter weight.");

 	age[i] = scannerObject.nextInt[];


}

System.out.println("\n");

System.out.println("Enter weight or -1 to end");

ageAverage = ageTotal / people; 	/*Calculates average age.*/

weight[i] = scannerObject.nextInt[];

for(int i = 0; weight[i]! = -1; i++)

{

weightTotal += weight[i];	 /*sums all weights entered*/

people ++; /*counts the number of people entered*/

System.out.println("Enter another weight or -1 to end");

weight[i] = scannerObject.nextInt[];

}

System.out.println("\n");

weightAverage = weightTotal / people;			/*Calculates average weight.*/

DecimalFormat twoDigits = new DecimalFormat("0.00");

{

Scanner scan = new Scanner (System.in);

						/*My attempt to value age high to low*/

int age[] = 0, lowest = 0, highest = 0;

for(int i=0;i<people;i++)

if (i == 0)

{

lowest = age[i];

highest = age[i];

} 			//end if

else if (age[i] < lowest)

lowest = age[i];

else if (age[i] > highest)

highest = age[i];

}

{

weight = scannerObject.nextInt();

Scanner scan = new Scanner (System.in);

int weight[] = 0, lowest = 0, highest = 0;
						/*My attempt to value weight high to low*/
for(int i=0;i<people;i++)

if (i == 0)

{

lowest = weight[i];

highest = weight[i];

} //end if

else if (weight[i] < lowest)

lowest = weight[i];

else if (weight[i] > highest)

highest = weight[i];



}

	/** Create new DecimalFormat object named twoDigits **/
/*Displays # of people entered,sum of age & weight, and average of age & weight.*/

		  System.out.println("You entered " + people + " people.");

		  System.out.println("The sum of your age is  " + ageTotal);

		  System.out.println("The sum of your weight is " + weightTotal);

		  System.out.println("Your average age is  " + twoDigits.format(ageAverage));

		  System.out.println("Your average weight is  " + twoDigits.format(weightAverage));

		  System.out.println("\n\n");

}				/*End of method for data.*/

public static void main(String[] args)

{				/*Main method.*/

People.peopleData();

}	/*End of main method.*/

}	/*End of class People*/




/* Sir- Please look at these statements. It will not read these in my program. What have
I done that I can correct? It reads 16 errors.Thanks*/

This post has been edited by xosunkist: 10 April 2007 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java: High and Low Value

Posted 10 April 2007 - 05:42 PM

Hi first I want to say that u can't expect just to copy/paste and back to utopia
ok now I'm saying this because you declare again same variable more than once like:
	   /*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;


and later on

						/*My attempt to value age high to low*/

int age[] = 0, lowest = 0, highest = 0;

for(int i=0;i<people;i++)

if (i == 0)

Same with
 Scanner scan = new Scanner (System.in);


Also I don't believe you know why you use weight[i] all over the code. I wanna help but I will NOT solve and post the code you need.
But I'll help. We'll go step by step:

1. Your code is fine to this point i.e. I suppose you understand it:
/*
   This program should allow the user to enter the age in years and weight in 
   pounds of an indefinite number of people. Have the user enter the ages and
   weights as integer values.Use a sentinal value of -1 when you are done 
   entering the information. Calculate the average weight  and age for all
   the people. Print the average age and weight of all the people to the 
   DOS screen. Print to the DOS screen the number of people on which information
   was entered. Print the highest age and the lowest age of all the ages
   entered. Print the highest weight and the lowest weight of all the
   weights entered.
*/

import java.util.*;

import java.text.DecimalFormat;

/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()	 /*Method for data*/

	  {
			/*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;



		Scanner scannerObject = new Scanner(System.in);

			/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");

		  System.out.println("and weight in pounds of an indefinite number of people.");

		  System.out.println("Enter the ages and weights as integer values.");

		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");

		  System.out.println("");

		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");

		  System.out.println("\n");


  Scanner scan = new Scanner (System.in);




OK, now the last line from the code above declares an object called scan of the class Scanner so there is no need to declare again such thing.

Now in the first for loop age[i]! = -2; should be age[i]!= -2;
and age[i] = scannerObject.nextInt[]; should be age[i] = scannerObject.nextInt();
there is a difference at least to the compiler. So what the program does in the first for loop is that it let the user to enter as many ages he/she likes and then calculates the total ages and the people.

Now in the second for loop weight[i] = scannerObject.nextInt(); should be inside the loop not outside because it doesn't make any sense if it's out. Also you shouldn't people++ again because you already know the number of the people from the first for loop. ok


Now you have two arrays filled with the info we need from the user
one array is: weight[] and the other is age[] You don't need

Quote

weight[i] = scannerObject.nextInt();

Scanner scanb = new Scanner (System.in);


you need just :
for(int i=0;i<people;i++)

if (i == 0)

{

lowestAge = age[i];

highestAge = age[i];

}			 //end if

else if (age[i] < lowestAge)

lowestAge = age[i];

else if (age[i] > highestAge)

highestAge = age[i];

}


and another one for the weight :
for(int i=0;i<people;i++)

if (i == 0)

{

lowestW = weight[i];

highestW = weight[i];

}			 //end if

else if (weight[i] < lowestW)

lowestW = weight[i];

else if (weight[i] > highestW)

highestW = weight[i];

}


lowestAge, highestAge, lowestW, highestW are four different variables
which shuold be declared in:
   public static void peopleData()	 /*Method for data*/

	  {
			/*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;



And for the output of the program just just add the additional
System.out.println("The highest age is"+highestAge); and the other respectively.
My hint to u is try to do this with understanding not just copy/paste
and never give up ;)
Was This Post Helpful? 0
  • +
  • -

#7 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 10 April 2007 - 10:19 PM

I learned a lot from the lesson. However, I don't know why this is still not compiling. I get 1 error now of illegal start of expression. What for?
public static void main(String[]args)

This I don't understand.

This post has been edited by xosunkist: 10 April 2007 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Java: High and Low Value

Posted 11 April 2007 - 02:03 AM

Well post the code and we'll see what the error is.
Was This Post Helpful? 0
  • +
  • -

#9 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 11 April 2007 - 10:34 AM

Okay here it is:

/*
  This program should allow the user to enter the age in years and weight in pounds of an indefinite
  number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
  of -1 when you are done entering the information. Calculate the average weight and age for all
  the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
  screen the number of people on which information was entered. Print the highest age and the lowest
  age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/

import java.util.*;

import java.text.DecimalFormat;




/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()	 /*Method for data*/

	  {
			/*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;

		  int lowestAge;

		  int highestAge;

		  int lowestW;

		  int highestW;



		Scanner scannerObject = new Scanner(System.in);

			/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");

		  System.out.println("and weight in pounds of an indefinite number of people.");

		  System.out.println("Enter the ages and weights as integer values.");

		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");

		  System.out.println("");

		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");

		  System.out.println("\n");


{ Scanner scan = new Scanner (System.in);

  for(int i = 0; age[i]!= -2; i++)


	ageTotal += age[i]; 	/*Sums all the ages entered.*/

	people ++; 	/* Counts the number of people entered */

	System.out.println("Enter another age or -2 to enter weight.");






System.out.println("\n");
{
System.out.println("Enter weight or -1 to end");

ageAverage = ageTotal / people; 	/*Calculates average age.*/



	for(int i = 0; weight[i]!= -1;i++)



weightTotal += weight[i];	 /*sums all weights entered*/

System.out.println("Enter another weight or -1 to end");




System.out.println("\n");

weightAverage = weightTotal / people;			/*Calculates average weight.*/

DecimalFormat twoDigits = new DecimalFormat("0.00");

{



						/*My attempt to value age high to low*/



for(int i=0;i<people;i++)

if (i == 0)

{

lowestAge = age[i];

highestAge = age[i];

} 			//end if

else if (age[i] < lowestAge)

lowestAge = age[i];

else if (age[i] > highestAge)

highestAge = age[i];

}

{


						/*My attempt to value weight high to low*/
for(int i=0;i<people;i++)

if (i == 0)

{

lowestW = weight[i];

highestW = weight[i];

} //end if

else if (weight[i] < lowestW)

lowestW = weight[i];

else if (weight[i] > highestW)

highestW = weight[i];

}

	/** Create new DecimalFormat object named twoDigits **/
/*Displays # of people entered,sum of age & weight, and average of age & weight.*/

		  System.out.println("You entered " + people + " people.");

		  System.out.println("The sum of your age is  " + ageTotal);

		  System.out.println("The sum of your weight is " + weightTotal);

		  System.out.println("Your average age is  " + twoDigits.format(ageAverage));

		  System.out.println("Your average weight is  " + twoDigits.format(weightAverage));

		   System.out.println("The highest age is" + highestAge);
		   System.out.println("The lowest age is" + lowestAge);
		   System.out.println("The highest weight is" + highestW);
		   System.out.println("The lowest weight is" + lowestW);

		  System.out.println("\n\n");

}				/*End of method for data.*/

public static void main(String[]args)

{				/*Main method.*/

People.peopleData();

}	/*End of main method.*/


}	/*End of class People*/





Was This Post Helpful? 0
  • +
  • -

#10 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 11 April 2007 - 04:44 PM

I was hoping someone could help me with this. I am getting 1 error message with this code below. of public static void main(String[]args).

I want to know how I can correct it but don't know where to begin.

Thanks

View Postxosunkist, on 11 Apr, 2007 - 10:34 AM, said:

Okay here it is:

/*
  This program should allow the user to enter the age in years and weight in pounds of an indefinite
  number of people. Have the user enter the ages and weights as integer values. Use a sentinal value
  of -1 when you are done entering the information. Calculate the average weight and age for all
  the people. Print the average age and weight of all the people to the DOS screen. Print to the DOS
  screen the number of people on which information was entered. Print the highest age and the lowest
  age of all the ages entered. Print the highest weight and the lowest weight of all the weights entered.*/

import java.util.*;

import java.text.DecimalFormat;




/*** Import Decimal Formating Class to format averages. ***/

public class People

  {

	  public static void peopleData()	 /*Method for data*/

	  {
			/*Variables*/

		  int people = 0;

		  double ageAverage = 0;

		  double weightAverage = 0;

		  int age[];

		  int weight[];

		  int ageTotal = 0;

		  int weightTotal = 0;

		  int lowestAge;

		  int highestAge;

		  int lowestW;

		  int highestW;



		Scanner scannerObject = new Scanner(System.in);

			/*Description of program for user.*/

		  System.out.println("The program should allow the user to enter the age in years");

		  System.out.println("and weight in pounds of an indefinite number of people.");

		  System.out.println("Enter the ages and weights as integer values.");

		  System.out.println("Use a sentinal value of -1 when you are done entering the information.");

		  System.out.println("");

		  System.out.println("\n\n");

		  System.out.println("Enter an age for a person and press ENTER:");

		  System.out.println("\n");


{ Scanner scan = new Scanner (System.in);

  for(int i = 0; age[i]!= -2; i++)


	ageTotal += age[i]; 	/*Sums all the ages entered.*/

	people ++; 	/* Counts the number of people entered */

	System.out.println("Enter another age or -2 to enter weight.");






System.out.println("\n");
{
System.out.println("Enter weight or -1 to end");

ageAverage = ageTotal / people; 	/*Calculates average age.*/



	for(int i = 0; weight[i]!= -1;i++)



weightTotal += weight[i];	 /*sums all weights entered*/

System.out.println("Enter another weight or -1 to end");




System.out.println("\n");

weightAverage = weightTotal / people;			/*Calculates average weight.*/

DecimalFormat twoDigits = new DecimalFormat("0.00");

{



						/*My attempt to value age high to low*/



for(int i=0;i<people;i++)

if (i == 0)

{

lowestAge = age[i];

highestAge = age[i];

} 			//end if

else if (age[i] < lowestAge)

lowestAge = age[i];

else if (age[i] > highestAge)

highestAge = age[i];

}

{


						/*My attempt to value weight high to low*/
for(int i=0;i<people;i++)

if (i == 0)

{

lowestW = weight[i];

highestW = weight[i];

} //end if

else if (weight[i] < lowestW)

lowestW = weight[i];

else if (weight[i] > highestW)

highestW = weight[i];

}

	/** Create new DecimalFormat object named twoDigits **/
/*Displays # of people entered,sum of age & weight, and average of age & weight.*/

		  System.out.println("You entered " + people + " people.");

		  System.out.println("The sum of your age is  " + ageTotal);

		  System.out.println("The sum of your weight is " + weightTotal);

		  System.out.println("Your average age is  " + twoDigits.format(ageAverage));

		  System.out.println("Your average weight is  " + twoDigits.format(weightAverage));

		   System.out.println("The highest age is" + highestAge);
		   System.out.println("The lowest age is" + lowestAge);
		   System.out.println("The highest weight is" + highestW);
		   System.out.println("The lowest weight is" + lowestW);

		  System.out.println("\n\n");

}				/*End of method for data.*/

public static void main(String[]args)

{				/*Main method.*/

People.peopleData();

}	/*End of main method.*/


}	/*End of class People*/





Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Java: High and Low Value

Posted 11 April 2007 - 05:16 PM

Can you post the exact error message?
Was This Post Helpful? 0
  • +
  • -

#12 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 11 April 2007 - 05:18 PM

View PostAmadeus, on 11 Apr, 2007 - 05:16 PM, said:

Can you post the exact error message?


Here is the exact error:


C:\cs151\People.java:195: illegal start of expression
public static void main(String[]args)
^
1 error

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#13 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 11 April 2007 - 06:37 PM

This is the error message below that I can not figure out how to correct it.

View Postxosunkist, on 11 Apr, 2007 - 05:18 PM, said:

View PostAmadeus, on 11 Apr, 2007 - 05:16 PM, said:

Can you post the exact error message?


Here is the exact error:


C:\cs151\People.java:195: illegal start of expression
public static void main(String[]args)
^
1 error

Tool completed with exit code 1

Was This Post Helpful? 0
  • +
  • -

#14 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: Java: High and Low Value

Posted 12 April 2007 - 05:32 AM

Can someone please, please help me on this?


View Postxosunkist, on 11 Apr, 2007 - 06:37 PM, said:

This is the error message below that I can not figure out how to correct it.

View Postxosunkist, on 11 Apr, 2007 - 05:18 PM, said:

View PostAmadeus, on 11 Apr, 2007 - 05:16 PM, said:

Can you post the exact error message?


Here is the exact error:


C:\cs151\People.java:195: illegal start of expression
public static void main(String[]args)
^
1 error

Tool completed with exit code 1

Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Java: High and Low Value

Posted 12 April 2007 - 05:43 AM

The problem may be that you have several opening braces that do not appear to have any purpose, such as:
{ Scanner scan = new Scanner (System.in);


another trouble area is that you begin for loops, and then do not properly block the code afterwards, even though it consists of several lines.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3