aspose file tools*
The moose likes Beginning Java and the fly likes declaring multiple variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "declaring multiple variables" Watch "declaring multiple variables" New topic
Author

declaring multiple variables

T Wilson
Greenhorn

Joined: Aug 28, 2007
Posts: 1
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!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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!


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.


    "The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    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.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: declaring multiple variables