Parameter passing

Hi all,

I'm currently trying out the parameter passing from one class to the other but can't seems to get it working. Appreciate if someone can enlighten me how can I read a data from the screen and pass it to another class? Would like to get this working and try out constructor

class Person {

int age;

void myAge(int newAge) {

age=newAge;

}

void printAge(){

System.out.println(age);

}

}

================================================

import java.io.*;

class PersonName{

public static void main (String[] args){

int a;

Person person1= new Person();

System.out.print("Enter age");

a = (int) System.in.read();

//person1.myAge(1);

person1.printAge(a);

}

}

[799 byte] By [camma] at [2007-10-3 3:00:00]
# 1

What exactly is your problem?

The only issue I can see is, that you didn't initialize your field in the Person class. So, when you call your print method without setting the field first, your behaviour might be unpredictable...

But besides this, the code is perfectly okay.

Btw, please use code tags.

Mongera at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 2
person1.myAge(1);person1.printAge();.
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 3

> The only issue I can see is, that you didn't

> initialize your field in the Person class. So, when

> you call your print method without setting the field

> first, your behaviour might be unpredictable...

It's very predictable. int fields get initialized with 0. Java is not C.

CeciNEstPasUnProgrammeura at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 4
I got an error as shown below. Which field is not being declared in the person's class?C:\java\>javac PersonName.javaPersonName.java:15: printAge() in Person cannot be applied to (int)person1.printAge(a);^1 error
camma at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 5

> I got an error as shown below. Which field is not

> being declared in the person's class?

Did you actually read the error message? Take a long good look at your code.

> C:\java\>javac PersonName.java

> PersonName.java:15: printAge() in Person cannot be

> applied to (int)

> person1.printAge(a);

>^

If you declare a method not to take an argument, you can't pass it an argument.

CeciNEstPasUnProgrammeura at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 6

Sorry this is my first time trying out java program and am trying out parameter passing. Somehow if I hardcode the value as shown below to the void statement, I can print the output of 1. However I'm confused of how I can pass the parameter if I read from the standard input. Appreciate if someone can enlighten me on this. Been reading up on this but can't seems to find an example on this. Most of the example are hardcoded. Thanks alot

person1.myAge(1);

camma at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 7
> person1.myAge(1);You know what a variable is, right? You can pass a variable the same way you pass the literal 1.int age = <get an int from user input or
jverda at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 8

Hi all,

Thanks for all your help. I've managed to get it to print char, but when I change to int, it'll give incorrect results. Such as when I input a 3, it'll output a 51. Is there anything I've missed?

public class Person {

private int a;

private int age;

public Person(int myAge)

{

age=myAge;

}

public int getAge(){

return age;

}

public void setAge(int newAge) {

age=newAge;

}

void printAge(int b){

age=b;

System.out.println(age);

}

}

import java.io.*;

class PersonAge{

public static void main (String[] args) throws IOException {

int a;

System.out.print("Enter age");

a = (int) System.in.read();

Person personOne = new Person(a);

personOne.printAge(a);

}

}

camma at 2007-7-14 20:49:37 > top of Java-index,Java Essentials,New To Java...
# 9

The problem is this line: a = (int) System.in.read();

That reads a byte. It does not interpret it as a character. If you type the letter 'A,' the byte that goes in has the value 65--the ASCII (and UTF-8) value of the letter 'A'. Likewise, the ASCII/UTF-8 value of the character '3' is 51.

You want to use java.util.Scanner.nextInt() or BufferedReader.readLine followed by Integer.parseInt. Google for java IO tutorial for details.

jverda at 2007-7-14 20:49:38 > top of Java-index,Java Essentials,New To Java...
# 10
void printAge(){System.out.println(age);}Since you set the age in the constructor or setAge method you don't need to pass the age again to the printAge method.
floundera at 2007-7-14 20:49:38 > top of Java-index,Java Essentials,New To Java...
# 11

Sounds to me like you're using the wrong method for converting the input String into an int. You see, 51 sounds like the ASCII code for the character '3'. Instead of casting the String as an int, you'll probably need to use the following code: replace

int a;

System.out.print("Enter age");

a = (int) System.in.read();

with

int a;

String b;

System.out.print("Enter age");

b = System.in.read();

a = Integer.valueOf(b).intValue();

What does that do, you might be wondering? Well, there's a special class called Integer, that works like int but has special features int doesn't have. One of those features is the ability to convert numbers stored in Strings into proper Integers. That's what the valueOf method does. Then, since you really need to work with the variable a as an int rather than an Integer, the intValue method turns it into a normal int.

Platonixa at 2007-7-14 20:49:38 > top of Java-index,Java Essentials,New To Java...
# 12

Hi all,

Thank You so much for your help, I've manged to get it working now. However i have one last question, hope you guys can enlighten me. :). For this code to compile, i need to pass in the parameter "a" when creating new instance like this.

Person personOne = new Person(a);

However, I've seen other examples that without passing in parameter, such is

Person personOne = new Person();

and no errors when compiling. Why is that so?

public class Person {

private int a;

private int age;

public Person(int myAge)

{

age=myAge;

}

public int getAge(){

return age;

}

public void setAge(int newAge) {

age=newAge;

}

void printAge(){

System.out.println(age);

}

}

import java.io.*;

class PersonAge{

public static void main (String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int a;

String b;

System.out.print("Enter age");

b=br.readLine();

a=Integer.valueOf(b).intValue();

Person personOne = new Person(a);

personOne.printAge();

}

}

camma at 2007-7-14 20:49:38 > top of Java-index,Java Essentials,New To Java...
# 13
Hi all, I've managed to figure this out. Thank You so much again.You guys are great !!!
camma at 2007-7-14 20:49:38 > top of Java-index,Java Essentials,New To Java...