Why can I not use variables in Java?

Why can I not use variables in Java?
I tried some fairly simple code:

import java.util.*;
//import java.util.Scanner;
//import java.lang.String;

public class ZellersCongruence {

public static void main(String args[]){

int DayOfWeek;
int DayOfMonth;
int Month;
int YearOfCentury;
int Century;
String Date;

Scanner inputVal = new Scanner(System.in);

System.out.println(“Please enter the day of the month”);
DayOfMonth = in.nextLine();

System.out.println(“Please enter number of the month”);
Month = in.nextLine();

System.out.println(“Please enter the year”);
YearOfCentury = in.nextLine();

And Command Prompt keeps spitting out:

C:\Documents and Settings\Jos\My Documents\Homework\College\Puting>javac Zellers
Congruence.java
ZellersCongruence.java:20: cannot find symbol
symbol : variable in
location: class ZellersCongruence
DayOfMonth = in.nextLine();
^
ZellersCongruence.java:23: cannot find symbol
symbol : variable in
location: class ZellersCongruence
Month = in.nextLine();
^
ZellersCongruence.java:26: cannot find symbol
symbol : variable in
location: class ZellersCongruence
YearOfCentury = in.nextLine();
^
3 errors

Any idea why this keeps happening? If you need the rest of the code then I can provide it :)
Thanks for reading :)
Thanks for the help, I was just pasting that bit from old code. However there’s a new problem, to do with incompatible types, something about expecting an integer :S

C:\Documents and Settings\Jos\My Documents\Homework\College\Puting>javac Zellers
Congruence.java
ZellersCongruence.java:19: incompatible types
found : java.lang.String
required: int
DayOfMonth = inputVal.nextLine();
^
ZellersCongruence.java:22: incompatible types
found : java.lang.String
required: int
Month = inputVal.nextLine();
^
ZellersCongruence.java:25: incompatible types
found : java.lang.String
required: int
YearOfCentury = inputVal.nextLine();
^
3 errors

C:\Documents and Settings\Jos\My Documents\Homework\College\Puting>

Best answer:

It’s not working because your variable (‘in’) doesn’t exist. You didn’t declare it anywhere.

Also, you commented out a few imports that are used in the program.

Tags: ,

Under Forum

2 Comments for Why can I not use variables in Java?

  • 1. Silent  |  April 8th, 2008 at 12:30 pm

    These error messages are telling you that the compiler can’t find a variable called “in”.

    You’ve crated a Scanner called “inputVal”, but when you try to use it, you keeping referring to it as “in” instead.

    Edit:
    Your second problem is pretty obvious. Scanner.nextLine() returns a String, but you’re trying to assign the return value to a variable of type int. Obviously that’s not going to work in a strongly typed language like Java. You need to convert the String to an int first, probably by using the Integer.parseInt() method, which you can read about at the link below.

  • 2. Noah  |  April 8th, 2008 at 1:26 pm

    You didn’t define ‘in’. From your code, it looks like you meant to write ‘inputVal.nextLine();’ instead of ‘in.nextLine();’.

    On a side note, you’ll want to use lowercase letters for the first word of your variable names, (month, dayOfMonth, and yearOfCentury). It’s not absolutely necessary to do so, but generally you only capitalize the first letter of the first word for class names. Hope that helps!

Leave a Comment for Why can I not use variables in Java?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories