• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

declaring multiple variables

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing a homework assignment. I am confused as to whether or not the stored variables will be saved, so do I create a new line for every item that I will need to recall later? that is what I have done, but do I need too? I have pasted what code have wriiten so far.(below)

Here is what it asks for:

Create a Java program called AverageWeight.java that uses the Scanner class to store the weights, expressed in pounds, of five people in appropriately named variables. The program should calculate the average of these weights and express the average in kilograms (1 pound = .454 kilograms.) The conversion factor should be stored in a named constant. Express the average to the nearest tenth of a kilogram. (Hint:To round a number to the nearest 0.1, add half that, 0.05, to the number, multiply it by 10, convert it to an integer, then divide it by 10.0. For example, 65.78 + .05 becomes 65.83, 65.83 times 10 is 658.3, then 658.3 converted to an integer is 658, and finally 658 divided by 10.0 is 65.8, which is 65.78 rounded to the nearest 0.1.) Output all the original and new information in the command window.

import java.util.Scanner;

public class AverageWeight
{
// main method
public static void main( String[] args )
{

//declare and initialize variables
String person1; person2; person3; person4; person5; allPersons, outputMessage;

int weight1, weight2, weight3, weight4, weight5, allWeights, totalWeight, averageWeight, KILO_COVERSION, kiloWeight;

final double KILO_CONVERSION = .454; // a named constant



//get required input
Scanner input = new Scanner( System.in );


System.out.print("Enter your name: " );
person1 = input.nextLine();

System.out.print("Person1 enter your weight in pounds: " );
weight1 = input.nextLine();

System.out.print("Enter your name: " );
person2 = input.nextLine();

System.out.print("Person2 enter your weight in pounds: " );
weight2 = input.nextLine();

System.out.print("Enter your name: " );
person3 = input.nextLine();

System.out.print("Person3 enter your weight in pounds: " );
weight3 = input.nextLine();

System.out.print("Enter your name: " );
person4 = input.nextLine();

System.out.print("Person4 enter your weight in pounds: " );
weight4 = input.nextLine();

System.out.print("Enter your name: " );
person5 = input.nextLine();

System.out.print("Person5 enter your weight in pounds: " );
weight5 = input.nextLine();




//convert average weights to kilograms (nearest tenth)
allPersons = person1, person2, person3, person4, person5; allWeight = weight1, weight2, weight3, weight4, weight5;
totalWeight = weight1 + weight2 + weight3 + weight4 + weight5;
avergeWeight = totalWeight/5;
addHalf = 0.05;
multiply = (* 10);
divide = (/ 10);
kiloWeight = averageWeight * KILO_CONVERSION + ADD_HALF + multiply + convert to integer + divide;



//output all original and new info
outputMessage = "Original names and weights in pounds are" allPersons + allWeights "there average weight in pounds equals" averageWeight "." /n;
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";



//print output to screen
System.out.println( outputMessage );

} // end main

} // end class


The errors that I am getting:

C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: not a statement
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:18: ';' expected
String person1; person2; person3; person4; person5; allPersons, outputMessage;
^
C:\JavaChp01\AverageWeight.java:64: ';' expected
allPersons = person1, person2, person3, person4, person5;
^
C:\JavaChp01\AverageWeight.java:65: ';' expected
allWeight = weight1, weight2, weight3, weight4, weight5;
^
C:\JavaChp01\AverageWeight.java:69: illegal start of expression
multiply = (* 10);
^
C:\JavaChp01\AverageWeight.java:69: ')' expected
multiply = (* 10);
^
C:\JavaChp01\AverageWeight.java:70: illegal start of expression
divide = (/ 10);
^
C:\JavaChp01\AverageWeight.java:70: ')' expected
divide = (/ 10);
^
C:\JavaChp01\AverageWeight.java:71: ';' expected
kiloWeight = averageWeight * KILO_CONVERSION + ADD_HALF + multiply + convert to integer + divide;
^
C:\JavaChp01\AverageWeight.java:76: ';' expected
outputMessage = "Original names and weights in pounds are" allPersons + allWeights "there average weight in pounds equals" averageWeight "." /n;
^
C:\JavaChp01\AverageWeight.java:77: not a statement
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";
^
C:\JavaChp01\AverageWeight.java:77: ';' expected
"The total average weight" averageWeight "in kilograms equals" kiloWeight ".";
^
16 errors

Tool completed with exit code 1


I am stuck! Any advise to help me learn what I am doing wrong would be greatly appreciated. I

Thanks!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch. Here's one hint: Compare this line you are getting lots of errors on:

to this line that does not have any errors:

See any difference?
I'm going to move your post to our beginner's forum, where you'll get more help than in this forum. Good luck!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe Ess has you going the right way.

  • change your displayed name to a first name and last name, we have to do this to keep things managable.
  • Initializer lists are separated by comma, your other init list works. this is why.
  • We do not do homework, your question is worded fine but be aware so you dont get disappointed
  • Whenever you get stuck, just fix the first few compiler errors. If that leaves you wondering what to do, just plug the wording of the compiler errors into your book's index and keep reading. That's how I got going,


  • Sometimes the ^ character from the compiler will be off from where the real error is. Look around the first compiler error, then look in your book.
     
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Nicholas Jordan:
    ...change your displayed name to a first name and last name...


    No need. The display name "T Wilson" is fine according to the JavaRanch Naming Policy. We allow initials for first names.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic