CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Array of string

  1. #1
    Join Date
    May 2013
    Posts
    1

    Array of string

    hello,am very new in java programming i have been given the assignment to write a program about the home affairs department, but am having a problem about how to make an array of string, below are the codes. I will be appreciative if i fully understand how arrays works.
    * @(#)HomeAffairs.java
    *
    *
    * @author
    * @version 1.00 2013/6/5
    */
    import java.util.Scanner;

    public class HomeAffairs {

    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    //DECLARING OF THE ARRAY VARIABLES
    String[]prsnlInfor={"FirstName","OtherNames","SurName","Age","Gender","BirthPlace","Country","Province","Applicant","DurationOfStay","Person",};
    int[]OtherInfor={BirthDate,DateIssued,ExpiryDate,OptionCode,AmountPaid};
    double[]cash={AmountPaid};

    //The user will need to enter the option code number to chose which activity
    //Birth certificate = Code 01, Identity card = Code 02, Passport book = Code 03
    System.out.print("Please enter the activity code : \n" + "Code 01: for Birth certificate \n" + "Code 02: for Identity \n" + "Code 03: for Passport");
    System.out.print("---------------------------------------------------------- \n");
    System.out.print("Select one of these activity codes \n");
    ActivityCode = input.nextInt();

    System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");

    switch (optionCode){

    case 01:
    System.out.println("Please enter the date of birth of the applicant");
    BirthDate=input.nextLine();

    System.out.print("---------------------------------------------------------- \n");
    System.out.println("Please enter the first name of the applicant");
    FirstName=input.nextLine();

    System.out.println("Please enter the other names of the applicant");
    OtherNames=input.nextLine();

    System.out.println("Please enter the gender of the applicant");
    Gender=input.nextLine();

    System.out.println("Please enter the birth place applicant");
    BirthPlace=input.nextLine();

    System.out.println("Please enter the country of origin applicant");
    Country=input.nextLine();

    System.out.println(",,,,,,,,,,,THANK YOU FOR PROVIDING THE INFORMATION ABOVE,,,,,,,,,,");

    break;
    case 02:
    System.out.println("please enter the applicant name");
    FirstName = input.nextLine();

    System.out.println("please enter the applicant other names");
    OtherName = input.nextLine();

    System.out.println("please enter the applicant gender");
    Gender = input.nextLine();

    System.out.println("please enter the applicant date of birth");
    BirthDate = input.nextLine();

    //The applicant of the identity card breen book must be above eighteen year of age
    for (int year=0;year<18;y++);
    System.out.println("year:" + year++);

    if (Age < 18){
    Applicant="under age";
    }
    else{
    Applicant="Qualified";
    }
    System.out.println("The applicant is : " + Applicant);

    //If the applicant is not a citizen of south africa then
    //he or she must have stayed in the country for morethan five years
    for (int year=0;year <5;year++);

    System.out.println("year : " + year++);
    if (DurationOfStay >= 5){
    Applicant = "Qualified";
    }
    else {
    Applicant = "Disqualified";
    }
    System.out.println("The applicant is : " + Applicant);

    //If the applicant is not a south african but marries a citizen of south africa,
    //then he or she is entitled to have an identity card
    if (Person =="marries a south african"){
    Applicant = "Qualified";
    }
    else {
    Applicant ="Disqualified";
    }
    System.out.println("The applicant is : " + Applicant);


    System.out.println("please enter the applicant place of birth");
    BirthPlace = input.nextLine();

    System.out.println("please enter the applicant country of origin");
    Country = input.nextLine();

    System.out.println(",,,,,,,,,,,THANK YOU FOR PROVIDING THE INFORMATION ABOVE,,,,,,,,,,");

    break;

    case 03:
    System.out.println("please enter the applicant name");
    FirstName = input.nextLine();

    System.out.println("please enter the applicant other names");
    OtherName = input.nextLine();

    System.out.println("please enter the applicant gender");
    Gender = input.nextLine();

    System.out.println("please enter the applicant date of birth");
    BirthDate = input.nextLine();

    //The applicant must have aleast an identity card to qualify for the passport
    if (Person =="holder of RSA ID"){
    Applicant ="Qualified";
    }
    else {
    Applicant = "Not qualified";
    }
    System.out.println("The applicant is : " + Applicant);

    //If the applicant is not a citizen of south africa then
    //he or she must have stayed in the country for morethan five years
    for (int year=0;year <5;year++);

    System.out.println("year : " + year++);
    if (DurationOfStay >= 5){
    Applicant = "Qualified";
    }
    else {
    Applicant = "Disqualified";
    }
    System.out.println("The applicant is : " + Applicant);

    //If the applicant is not a south african but marries a citizen of south africa,
    //then he or she is entitled to have an identity card
    if (Person =="marries a south african"){
    Applicant = "Qualified";
    }
    else {
    Applicant ="Disqualified";
    }
    System.out.println("The applicant is : " + Applicant);


    System.out.println("please enter the applicant place of birth");
    BirthPlace = input.nextLine();

    System.out.println("please enter the applicant country of origin");
    Country = input.nextLine();

    System.out.println(",,,,,,,,,,,,,,,THANK YOU FOR PROVIDING THE INFORMATION ABOVE,,,,,,,,,,");



    }
    }


    }

  2. #2
    Join Date
    Apr 2013
    Posts
    2

    Re: Array of string

    String[]prsnlInfor={"FirstName","OtherNames","SurName","Age","Gender","BirthPlace","Country","Province","Applicant","DurationOfStay","Person",}

    This is making an array of String with 11 indexes or a length of 11 (with the range of 0 - 10) and you are placing each of the strings you have in between {} into them, one into each index.
    i.e. the String "FirstName" is going actually being referenced at index 0 of prsnlInfor.

    You can leave it like that or comment what each index is and just make a String array of length 11 with....

    String[]prsnlInfor= new String[11];

    Now when you want to put a String into your array you need to make sure you are telling it which array and index you are placing it into. Something like....

    prsnlInfor[0] = input.nextLine();

    this will get the user's next input and have it referenced from index 0 of prsnlInfor.

    You were saying that your not 100% sure how arrays work. I found this picture which I think does a good job at showing what is going on. Not sure if this site allows links or not but I will try it.

    http://www-acad.sheridanc.on.ca/~jol...1_arrayMem.jpg

    What is actually store in grades[] is just a memory reference to where the actual information is stored.

    In your case "int[]otherInfor= new int[5];" will go and reserve enough memory for 5 int, sets them to its default which if I remember correctly is 0 and sends back the starting memory location to the variable otherInfor. Now otherInfor is storing the memory location of where the 5 new integers are located and itself is not actually storing the integers.

    I hope this helps. I would recommend looking up reference variables, it will help understand how arrays work.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured