The error is here:
...
Age=(int)System.in.read(); // you put 10
System.out.println("Age is="+Age); // Age is 49
...
You get the ascii code of character instead of the number.
http://en.wikipedia.org/wiki/ASCII#ASCII_control_characters
You must to change the code.
for example like this:
http://www.java2s.com/Code/Java/File-Input-Output/UsingBufferedReadertoreadinputnumberfromuser.htm
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
double number = Double.parseDouble(input);
System.out.println(input + ":" + Math.sqrt(number));
reader.close();
}
}
or in another way.
--- Ven 21/8/09, Ashok A V <[email protected]> ha scritto:
Da: Ashok A V <[email protected]>
Oggetto: [java programming] Re: Somebody help me out.
A: [email protected]
Cc: [email protected]
Data: Venerdì 21 agosto 2009, 18:13
Hi Elvis ,
Your Employment class should look like this :
import java.util.Scanner;
public class Employment {
public static void main(String[] args) throws Exception {
Employee[] Worker = new Employee[5];
int Age;
int Number;
double Salary;
int x;
Scanner input = new Scanner(System.in);
System.out.println("This is for five workers in my company" + "\n");
for (x = 0; x < 5; x++) {
Worker[x] = new Employee();
System.out.println("\nWorker" + (x + 1) + "\n");
System.out.println("Enter your age:");
Age = input.nextInt();
Worker[x].setEmpAge(Age);
System.out.println("Worker's Number:");
Number = input.nextInt();
Worker[x].setEmpNum(Number);
System.out.println("Worker's Salary:");
Salary = input.nextDouble();
Worker[x].setEmpSalary(Salary);
}
System.out.println("--------------------------------------------------------------------");
for (x = 0; x < 5; x++) {
System.out.println("\n worker :" + (x + 1) + "\n");
System.out.println("Age:" + Worker[x].getEmpAge() + "\n Number:"
+ Worker[x].getEmpNum() + "\n Salary:"
+ Worker[x].getEmpSalary());
}
}
}
and your Employee class like this :
public class Employee {
private int EmpNum;
private double EmpSalary;
private int EmpAge;
public Employee(){
}
public Employee(int num, double sal, int age) {
this.EmpNum = num;
this.EmpSalary = sal;
this.EmpAge = age;
}
public int getEmpNum() {
return EmpNum;
}
public void setEmpNum(int empNum) {
EmpNum = empNum;
}
public double getEmpSalary() {
return EmpSalary;
}
public void setEmpSalary(double empSalary) {
EmpSalary = empSalary;
}
public int getEmpAge() {
return EmpAge;
}
public void setEmpAge(int empAge) {
EmpAge = empAge;
}
}
Thanks,
Ashok A V
On Fri, Aug 21, 2009 at 9:44 PM, elvis sarfo<[email protected]> wrote:
> Hi, i don't know what i did not do right here.
> i created two classes; Employee and Employment and i and then i created an
> array of 5 objects of the Employee class but the output that i am getting is
> errorneous for me. Please help me . This is the code:
>
> public class Employee
> {
> private int EmpNum;
> private double EmpSalary;
> private int EmpAge;
> public Employee(int num, double sal, int age)
> {
> this.EmpNum=num;
> this.EmpSalary=sal;
> this.EmpAge=age;
> }
> public int Age()
> {
> return EmpAge;
> }
> public double Salary()
> {
> return EmpSalary;
> }
> public int Num()
> {
> return EmpNum;
> }
> }
>
>
> public class Employment
> {
> public static void main(String[] args) throws Exception
> {
> Employee[] Worker= new Employee[5];
> int Age;
> int Number;
> double Salary;
> int x;
> System.out.println("This is for five workers in my company"+"\n");
> for(x=0;x<5;x++)
> {
> System.out.println("\nWorker"+(x+1)+"\n");
> System.out.println("Enter your age:");
> Age=(int)System.in.read();
> System.in.read();System.in.read();
> System.out.println("Worker's Number:");
> Number=(int)System.in.read();
> System.in.read();System.in.read();
> System.out.println("Worker's Salary:");
> Salary=(double)System.in.read();
> System.in.read();System.in.read();
> Worker[x]=new Employee(Number,Salary,Age);
> }
> System.out.println("--------------------------------------------------------------------");
> for(x=0;x<5;x++)
> {
> System.out.println("\n worker :"+(x+1)+"\n" );
> System.out.println("Age:"+Worker[x].Age()+"\n Number:"+Worker[x].Num()+"\n
> Salary:"+Worker[x].Salary());
> }
> }
> }
> Thank you.
>
> >
>
--
Victory belongs to the most persevering.
- Napoleon
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---