LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java does "age" or "Age" matter when declaring an Int?? (https://www.linuxquestions.org/questions/programming-9/java-does-age-or-age-matter-when-declaring-an-int-103279/)

Laptop2250 10-12-2003 10:42 PM

Java does "age" or "Age" matter when declaring an Int??
 
Hey this is my code, :newbie: stuff:

public class GMFnTFF
{public static void main (String []args)
{EasyReader r= new EasyReader ();
int age;
System.out.print ("Enter your name: ");
String name= r.readLine ();
System.out.print ("Enter your age: ");
String LineAge= r.readLine ();
int Age= Integer.parseInt (LineAge); ////here i tried doing age why
////doesnt that work? but Age does
System.out.print ("Enter your weight: ");
String weight1= r.readLine ();
double weight= Double.parseDouble (weight1);
System.out.print ("Greetings, "+name+". You are "+Age+" years old and you weight "+weight+" pounds.");
}
}



int Age works
int age doesnt

why? what does age do in java? I am using j2sdk1.4.2 using the simple compiler bluejay and i am new if you cant tell so try to explain it easily. thx

thanks in advance (ey, me crew do yourselves a fava and dont steal my program or else, ali g talk :D)

also, i was updating this code for the last 1/2hr or so, and i found out that when i would type in

String name= r.readLine ();

it would automatically print the line to the output, does this always happen auto or is there a way to save a string and not print it immediatly? thanks again in advance (also the ali g talk meant dont steal my code, lol)

ksgill 10-13-2003 12:01 AM

Age and age are 2 different things because variables in java are case sensitive. age is just a variable and doesnt do anything in java.
String name= r.readLine ();
it doesnt print to output, it reads a line and saves it under a string name. ..looks like u just got started with programming.. I think you should read a good book if u want to learn java.
cool :)

Laptop2250 10-13-2003 09:54 AM

i try to make age a int, so your telling me all ints have to be lower case? i tried bge (lower case int) and that works, i guess i didn't understand, is age a reserved method(or something) like double?

also, String name= r.readLine (); saves a variable, but why does it print it to the output if i didn't ask it to. the output of this code is

Enter your name: Laptop2250
Enter your age: 2250
Enter you weight: 225
Greetings, Laptop 2250. You are 2250 years old and you weight 225 pounds.

the only thing i understand is that those strings/int/doubles should only appear in the last line (greetings) because i told the program to. why do the ints/strings/doubles appear other places?

thanks again

orgcandman 10-13-2003 12:34 PM

Here's my code that works (I changed the EasyReader class because I have an older JVM)

I think the problem is areound the line that I've flagged with /* >>> */
I think, judging from your code, you had declared age as an int at line /* LINE REF A */ and then
re-declared it as an int on line /* >>> */ which is a nono...
PS: I also used System.out.println because it will cause the output stream buffer to flush() automatically. plus it adds a newline character.


Code:


import java.io.*;
import java.util.*;
import java.lang.String;

public class GMFnTFF{
    public static void main (String []args) throws IOException{
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        /* LINE REF A */ int age;
        System.out.println("Enter your name: ");
        String name= r.readLine ();
        System.out.println("Enter your age: ");
        String LineAge= r.readLine ();
        /* >>> */ age = Integer.parseInt (LineAge);    // I used age and it worked :D
        System.out.println("Enter your weight: ");
        String weight1= r.readLine ();
        double weight= Double.parseDouble (weight1);
        System.out.println("Greetings, "+name+". You are "+age+" years old and you weight "+weight+" pounds.");
    }
}

Aaron


All times are GMT -5. The time now is 02:41 AM.