Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Writing data to an array

    So flag is a variable and we initialised it so it started being false but if its true, that means the name was found ?

  2. #22
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Writing data to an array

    Yes, it is a variable and set it to false at the beginning notify that the name is not found. Once a name is found on the array we set it to true. Later we can use that variable to work on.

  3. #23
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Writing data to an array

    Okay so I understand that now and when I used your code it worked perfectly and the only thing I was wondering is if it is possible to save the new entry into the array permanently, because if I run the program again it returns it back to its original state. Like if I add one customer, and then later on I want to add another customer the array is reset to its original position and the customer I added earlier is gone.
    Thank you so much for you help.

  4. #24
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Writing data to an array

    No, you never able to do that. Because all of these happen in your PC memory, not on a permanent storage. If you really want to do that you have to deal with a permanent storage, like text file, databases, etc. In your case a text file is more suitable. Basically you have to read a text file and fill the array initially and write to the file for non-existent names.

  5. #25
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Writing data to an array

    Alright I think that is beyond what I need to do for this. Well thank you very much for all of your help. I'll see if I can get this done now :)

  6. #26
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #27
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Writing data to an array

    Sorry to bother you again Eranga but I have come across a problem now. I have re written my code and I did not put in the boolean flags because I wasn't quite sure how to and whenever I try to save a name to the array it completly disregards my if statment and goes directly to my else and I'm not sure why, do you think you could help me.

    Java code:

    Java Code:
    import java.util.*;
    
    public class test3
    {
        private String customerName[];
        private static int size = 20;
    
        public test3()
        {
            customerName = new String[size];
            Scanner in = new Scanner(System.in);
            for(int i = 0; i <5; i++)
            {
                System.out.println("Enter the customer names: ");
                customerName[i] = in.nextLine();
            }
        }
        
        public void searchName()
        {
            int index = 0;
            String name;
            Scanner in = new Scanner(System.in);
            System.out.println("What is the customers name: ");
            name = in.nextLine();
            while (index <customerName.length && !name.equals(customerName[index]))
            {
                index++;
            }
            if (index == customerName.length)
            {
                System.out.println(name+ " not found.");
                if (index <customerName.length && customerName[index] == null)
                {
                    customerName[index] = name;
                }
                else
                {
                    System.out.println("This is broken");
                }
            }
            else
            {
                System.out.println(name+ " found at " +index);
            }
        }
    }
    Last edited by Eranga; 12-14-2011 at 02:01 PM. Reason: code tags added

  8. #28
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Writing data to an array

    You can find the complete solution on my code posted earlier. So why are you stick with this?

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Writing data to an array

    And also please use the code tags when you are posting code segments next time. Unformatted codes are really hard to read.

  10. #30
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Writing data to an array

    I just thought I would try and get the same results on my own and how do I use the code tag things?
    Thank you :)

  11. #31
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Writing data to an array

    So what you should do is identify the logic I implement and the logic you have already implemented. Then compare them, you can get a better understanding.

    Regarding the code tags, please have a look at my forum signature, there is a link.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Writing the data into RandomAccessFile
    By vasavi.singh in forum New To Java
    Replies: 5
    Last Post: 01-31-2013, 08:12 AM
  2. Need Help - Writing Data to File
    By uday.bhagwat7 in forum New To Java
    Replies: 0
    Last Post: 02-17-2011, 05:13 AM
  3. Writing data into a file Please Help!
    By Richman118 in forum New To Java
    Replies: 3
    Last Post: 12-17-2010, 03:28 AM
  4. Writing Data or Objects?
    By Goodwine in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 01:36 AM

Posting Permissions

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