# Java ";" Expected and illegal start of expression



## dtwins (Feb 3, 2012)

Hey guys, need help with java not compiling, here is the code and errors.

/** PROGRAM ASSIGNMENT 1 DaltonBrandonP2.java
*
* Author: Dalton Brandon
* Class: Java, Online
* Due Date: February 8, 2012
* See if roof needs changed, calculate price per square foot
*/

import java.util.Scanner;

public class DaltonBrandonP2 {
public static void main(String[] args) {
System.out.println("Dalton Brandon, Program Assignment 2, Java, online");
//New Scanner
Scanner input = new Scanner(System.in);

while (again == 1) {
//Year house was built
System.out.print("What year was the house built?: ");
int year = input.nextint();

//Square foot of house
System.out.print("Enter square feet of house: ");
double square = input.nextdouble();

//Total price
System.out.print("Enter total price: ");
double price = input.nextdouble();

//Does roof need changing?
void ifRoofChangeNeeded(int year){
if ((2012 - year) >= 15)
System.out.print("Roof change is needed");
else
System.out.print("No roof change is needed");
}
//Calculate price per square foot
void calcPricePersqf(double square, price){
int priceSqrf = price / square;
System.out.print("Price per square foot is: " + priceSqrf);
return priceSqrf;
}

System.out.print("Work on another house? 1 for YES, 2 for NO: ");
int again = input.nextint();
}
}
}

ERRORS

C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:32: error: illegal start of expression
void ifRoofChangeNeeded(int year){
^
C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:32: error: ';' expected
void ifRoofChangeNeeded(int year){
^
C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:32: error: ';' expected
void ifRoofChangeNeeded(int year){
^
C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:39: error: illegal start of expression
void calcPricePersqf(double square, price){
^
C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:39: error: ';' expected
void calcPricePersqf(double square, price){
^
C:\Users\Dalton Brandon\Desktop\JAVA\Assignment 2\DaltonBrandonP2.java:39: error: ';' expected
void calcPricePersqf(double square, price){
^
6 errors

Tool completed with exit code 1
---------------------------------------------------------------------------------------------------------

Am using TextPad, required for class


----------



## iansjack (Nov 28, 2010)

You are defining functions (ifRoofChanged(), etc.) within your main() functions. I believe this is not allowed in Java. Move the definitions outside of the main() function and try again.


----------



## sepala (May 20, 2010)

are you sure this is the complete code? Where is the "again" variable?


----------



## dtwins (Feb 3, 2012)

At the very end, its the last line of actual text


----------



## dtwins (Feb 3, 2012)

How do i declare the methods outside the main function but still keep it inside the loop?


----------



## sepala (May 20, 2010)

OK.. I can see the issues now.

1. Bring the last variable "again" to the beginning of the code, or assign another variables value to it and the end
Ex:* int a = input.nextInt();
again = a;*

2. Follow Java variable and method naming conventions. The first word of a method should be simple and the first letter of the second word should be capital. So, it is* input.nextInt() not input.nextint();
*
3. All the variables inside a method should be initialized first. So, at the beginning of the main method, initialize them *ex: int a = 0; String word=null

*4. Take methods outside of the main method

5. Learn about return types. Void methods doesn't return anything.


----------



## sepala (May 20, 2010)

dtwins said:


> How do i declare the methods outside the main function but still keep it inside the loop?


Main method can access static resources only. So the outside methods has to be static too.

For an example
*
public static String firstMethod(String command)
{
System.out.println(command);
};*
*
public static void main(String[]args)
{
while(true)
{
 String a = firstMethod("I am a Command");
System.out.println(a);
}
}*


----------



## dtwins (Feb 3, 2012)

That all makes sense, but im required to keep all code within the LOOP


----------



## sepala (May 20, 2010)

Then you can't use other methods. Because you can't create a method inside another method


----------



## iansjack (Nov 28, 2010)

I think that you must have misunderstood your assignment. You cannot possibly keep all the code within the loop. And why bother with procedures or functions that are called only once; just make them inline code.

Really I think you need to discuss this with your teacher as I suspect you have misunderstood what is being asked of you.


----------

