5 Replies - 120 Views - Last Post: 06 August 2012 - 06:53 AM Rate Topic: -----

#1 sengoku03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-June 12

Compilation error

Posted 06 August 2012 - 06:23 AM

Hello guys I'm a newbie in java I hope you can give me a hand on my problem in my assignment

here is the code:

import java.io.*;
import java.util.*;

class Overtime{
public static void main(String[] args)throws Exception{

Scanner q=new Scanner(System.in);
System.out.println("\n<-----------------------++++----------------------->");
System.out.print("->IDNO: ");
String idno=q.nextLine();

Scanner ket = new Scanner(new File("Employee.txt"));
String a="";

while(ket.hasNext()){
a=ket.next();
String reg[]=a.split(",");

if(idno.equals(reg[0])){
System.out.println("->Name: " + reg[1]);
System.out.println("->Rate: " + reg[2]);
System.out.println("\n<-----------------------++++----------------------->");


Scanner b=new Scanner(System.in);

System.out.print("Time-in for Monday: ");
String min=b.nextLine();
System.out.print("Time-out for Monday: ");
String mout=b.nextLine();
System.out.print("Overtime?: ");
String overm=b.nextLine();

 if(overm.equals("yes")){

	System.out.print("Time-in: ");
	String omin=b.nextLine();
	System.out.print("Time-out: ");	
	String omout=b.nextLine();

}

System.out.print("Time-in for Tuesday: ");
String tin=b.nextLine();
System.out.print("time-out for Tuesday: ");
String tout=b.nextLine();
System.out.print("Overtime?: ");
String overt=b.nextLine();



 if(overt.equals("yes")){

	System.out.print("Time-in: ");
	String otin=b.nextLine();
	System.out.print("Time-out: ");	
	String otout=b.nextLine();

}

System.out.print("Date Coverage: ");
b.nextLine();
System.out.println("\n<-----------------------++++----------------------->");



System.out.println("Total Hours");

//**Compute Regular Time**\\
	double regmon= Double.parseDouble(mout) - Double.parseDouble(min) -1;
	double regtue= Double.parseDouble(tout) - Double.parseDouble(tin) -1;
	double totalreg = regmon + regtue;
System.out.println("  ->Regular Time: " + totalreg);



//**Compute Overtime**\\
	double ovmon= Double.parseDouble(omout) - Double.parseDouble(omin);
	double ovtue= Double.parseDouble(otout) - Double.parseDouble(otin);
	double totalover = ovmon + ovtue;
System.out.println("  ->Overtime: " + totalover);

}
}

}
}



The problem is when I run the program it gives me 4 errors with same description
it says cannot find symbol
like this:

Overtime.java:78: error: cannot find symbol
        double ovmon= Double.parseDouble(omout) - Double.parseDouble(omin);
                                         ^
  symbol:   variable omout
  location: class Overtime
Overtime.java:78: error: cannot find symbol
        double ovmon= Double.parseDouble(omout) - Double.parseDouble(omin);
                                                                     ^
  symbol:   variable omin
  location: class Overtime
Overtime.java:79: error: cannot find symbol
        double ovtue= Double.parseDouble(otout) - Double.parseDouble(otin);
                                         ^
  symbol:   variable otout
  location: class Overtime
Overtime.java:79: error: cannot find symbol
        double ovtue= Double.parseDouble(otout) - Double.parseDouble(otin);
                                                                     ^
  symbol:   variable otin
  location: class Overtime
4 errors


the error describes the computation of Overtime
any advice?

*Edited: topic title changed to be more descriptive

This post has been edited by pbl: 06 August 2012 - 06:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Compilation error

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,234
  • Joined: 30-January 11

Re: Compilation error

Posted 06 August 2012 - 06:28 AM

You do not have variables called omout and otout in access.

I also suggest you rename all you variables to something more descriptive. At the moment it's a real mess.

This post has been edited by Ryano121: 06 August 2012 - 06:31 AM

Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Compilation error

Posted 06 August 2012 - 06:29 AM

Look here:
 if(overm.equals("yes")){

	System.out.print("Time-in: ");
	String omin=b.nextLine();
	System.out.print("Time-out: ");	
	String omout=b.nextLine();

}

You defined your variables inside an if block, so those variable will be only accessible locally inside that block and cant be accessed outside that block. That is why you get an error "cant find symbol".
To solve this problem you have to remove the declaration of those variables and put them outside the if block so they can be seen in the whole method.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Compilation error

Posted 06 August 2012 - 06:29 AM

It is a scope problem

if(overm.equals("yes")){  
     System.out.print("Time-in: ");  
     String omin=b.nextLine();  
     System.out.print("Time-out: ");   
     String omout=b.nextLine();     // <--- omout starts to exist here
}  // <--- and stops to exists here



you will have to declare it outside so that everybody can see it
Was This Post Helpful? 1
  • +
  • -

#5 jvdubn  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 08-June 11

Re: Compilation error

Posted 06 August 2012 - 06:45 AM

Try initializing your variables at the beginning. This helps to keep track of your variable names and ensures that the scope of the variable reaches outside of the braces.


class Overtime{
public static void main(String[] args)throws Exception{
	
String omout = "";//init vars
String omin = "";
String otout = "";
String otin = "";


 if(overm.equals("yes")){

	    System.out.print("Time-in: ");
	omin=b.nextLine();//get omin
	    System.out.print("Time-out: ");	
        omout=b.nextLine();//get omout

}



 if(overt.equals("yes")){

	System.out.print("Time-in: ");
    otin=b.nextLine();
	System.out.print("Time-out: ");	
    otout=b.nextLine();

}



Was This Post Helpful? 1
  • +
  • -

#6 sengoku03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-June 12

Re: Compilation error

Posted 06 August 2012 - 06:53 AM

I got it now thanks anyway and sorry for not much descriptive on my variables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1