Results 1 to 5 of 5
  1. #1
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Question 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

    PHP 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
    This is the error i got


    :confused:
    I LOVE JAVA!

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Well first, there is no nextString() method within the Scanner or String classes. So I would suggest:
    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();
    }
    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.

    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)

  3. #3
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    I'm doing a driver and a class file

    here's my class file
    PHP 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
    and here is my driver file

    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
    this is the output i must get :



    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 file
    Last edited by Deon; 01-12-2008 at 09:44 AM.
    I LOVE JAVA!

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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)

  5. #5
    Deon's Avatar
    Deon is offline Member
    Join Date
    Jan 2008
    Posts
    12
    Rep Power
    0

    Default

    thank you! captain morgan i will try my best if i got any more question i will ask u again THX!:)
    I LOVE JAVA!

Similar Threads

  1. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 12:02 AM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 11:55 PM
  3. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM
  4. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 08:13 AM
  5. i can't understand using interface as a type
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •