Results 1 to 5 of 5
Thread: i don understand this error
- 01-12-2008, 05:59 AM #1
i don understand this error
Hi guys please help me slove this problem =D
this is my half way yet to complete code but i already got some error
This is the error i gotPHP Code:import java.util.Scanner; public class PersonApp{ public static void main(String args[]){ String name, Gender, icNo; Integer DOB; Scanner input = new Scanner(System.in); System.out.print("Enter your name :"); name = input.nextstring (); System.out.print("Enter your date of birth (dd/mm/yyyy) :"); DOB = input.nextInt (); System.out.print("Enter your gender :"); Gender = input.nextString (); System.out.print("Enter your IC number :"); icNo=input.nextInt (); }//Main }//class

:confused:I LOVE JAVA!
- 01-12-2008, 06:59 AM #2
Well first, there is no nextString() method within the Scanner or String classes. So I would suggest:
And that should take care of reading your string inputs. As far as icNo, I'm unclear whether you wish to retrieve a string from the input, or whether icNo is really supposed to be declared as an int(which it's not). If you want it to return an int, declare as one instead of a string. If it's really supposed to be a string - follow the method above for now, but find a better way as readLine() is deprecated.Java Code:String name; DataInputStream in = new DataInputStream(System.in); System.out.print("Enter your name :"); try { name = in.readLine(); } catch (Exception e) { e.printStackTrace(); }
See how that works for you...Last edited by CaptainMorgan; 01-12-2008 at 07:05 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 01-12-2008, 09:37 AM #3
I'm doing a driver and a class file
here's my class file
and here is my driver filePHP Code:/* person ============== -name: String -DOB : Integer -icNo: String -Gender: String =============================== + findAge():int + setName(name:String):void + setDOB(DOS:Integer):void + seticNo(icNo:String):void + setGender(gender:String):void + getName():String + getDOB():integer + geticNo():String + getGender():String */ public class person{ private String name; private String DOB; private String icNo; private String Gender; public person(String na, String ic,String date, String sex){ name = na; icNo = ic; DOB = date; Gender = sex; } public void setName(String na){ name = na; } public void seticNo(String ic){ icNo = ic; } public void setDOB(String date){ DOB = date; } public void setGender(String sex){ Gender = sex; } public String getName(){ return name; } public String geticNo(){ return icNo; } public String getDOB(){ return DOB; } public String getGender(){ return Gender; } public int findAge(){ String b = icNo.substring(1,3); int c = Integer.parseInt(b); c = 1900 + c; int age = 2008 - c ; return age; }//main }//class
this is the output i must get :PHP Code:import java.util.Scanner; public class PersonApp{ public static void main(String args[]){ String name,DOB,Gender,icNo; Scanner input = new Scanner(System.in); System.out.print("Enter your name :"); name = input.nextLine (); System.out.print("Enter your date of birth (dd/mm/yyyy) :"); DOB = input.nextLine (); System.out.print("Enter your gender :"); Gender = input.nextLine (); System.out.print("Enter your IC number :"); icNo = input.nextLine (); icNo.seticNo(icNo); System.out.println( name + " is " + icNo.findAge()+ " years old. "); System.out.println("Gender is " + Gender + " and IC number is " + icNo.geticNo()); }//Main }//class

but somehow i don really know how to make use of the seticNo can anyone teach mi how to use it?
i already got a formula to find the age in my class fileLast edited by Deon; 01-12-2008 at 09:44 AM.
I LOVE JAVA!
- 01-12-2008, 09:54 AM #4
You appeared to make a valid effort, thus I've made the necessary corrections - although do not pass it in as is, you must review and edit where necessary.
In the PersonApp class you did not instantiate an object. This neglect made the constructor and Person class's methods virtually useless. I suggest you review your object creation and object use sections in your textbook. Objects and classes are the heart and soul of Java, you must learn these concepts through and through.
Best of luck! And if you're still not clear, let me know. ;)
** also, after a test run or two, your age calculation is not accurate, but I'll leave that to you. :)
Java Code:/* person ============== -name: String -DOB : Integer -icNo: String -Gender: String =============================== + findAge():int + setName(name:String):void + setDOB(DOS:Integer):void + seticNo(icNo:String):void + setGender(gender:String):void + getName():String + getDOB():integer + geticNo():String + getGender():String */ public class Person{ private String name; private String DOB; private String icNo; private String Gender; // four param constructor public Person(String na, String date, String ic, String sex){ name = na; DOB = date; icNo = ic; Gender = sex; } // setters public void setName(String na){ name = na; } public void setDOB(String date){ DOB = date; } public void seticNo(String ic){ icNo = ic; } public void setGender(String sex){ Gender = sex; } // getters public String getName(){ return name; } public String getDOB(){ return DOB; } public String geticNo(){ return icNo; } public String getGender(){ return Gender; } // age calculator public int findAge() { String b = icNo.substring(1,3); int c = Integer.parseInt(b); c = 1900 + c; int age = 2008 - c ; return age; } }Java Code:import java.util.Scanner; public class PersonApp { public static void main(String args[]){ String name, dob, gender, icNo; Scanner input = new Scanner(System.in); System.out.print("Enter your name :"); name = input.nextLine (); System.out.print("Enter your date of birth (dd/mm/yyyy) :"); dob = input.nextLine (); System.out.print("Enter your gender :"); gender = input.nextLine (); System.out.print("Enter your IC number :"); icNo = input.nextLine (); // *** instantiate an object! *** // *** this is why we created the constructor *** Person p = new Person(name, dob, icNo, gender); // *** now we can use it!! System.out.println(p.getName() + " is " + p.findAge() + " years old. "); System.out.println("Gender is " + p.getGender() + " and IC number is " + p.geticNo()); } }Last edited by CaptainMorgan; 01-12-2008 at 09:57 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 01-12-2008, 10:03 AM #5
Similar Threads
-
help me need to understand queries
By hossainsadd in forum JDBCReplies: 1Last Post: 05-26-2008, 12:02 AM -
Errors I don't understand
By MattyB in forum New To JavaReplies: 4Last Post: 04-01-2008, 11:55 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
New: Want to understand Drawing...
By diRisig in forum New To JavaReplies: 1Last Post: 02-05-2008, 08:13 AM -
i can't understand using interface as a type
By sireesha in forum New To JavaReplies: 3Last Post: 11-20-2007, 10:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks