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

    Default Writing data to an array

    Hi everyone
    I'm having an issue with my code where I use a scanner to input a String which is then searched through an array. If the string is present in the array, were told that the String already exists. If the String is not present in the array, then the new String is added to a null position within the array. The issue is that I am unable to make it work where the String gets added to the array, everything else works fine.
    Any help would be greatly appreciated.

    Here is my code:
    Java Code:
    import java.util.*;
    
    public class test
    {
        private static int size = 20;
        private String customerName[];
    
        public void customerName()
        {
            customerName = new String[size];
            customerName[0] = "Joshua";
            customerName[1] = "Sandee";
            customerName[2] = "Sam";
            customerName[3] = "Laure";
            customerName[4] = "Joseph";
            int place = 0;
            String name;
            Scanner in = new Scanner(System.in);
            System.out.print("What is the customers name: ");
            name = in.nextLine();
            while (place <customerName.length && !name.equals(customerName[place]))
            {
                place++;
            }
            if (place == customerName.length)
            {
                if (place <customerName.length && !name.equals(customerName[place]) && customerName[place].equals(null))
                {
                    name = customerName[place];
                }
                
                else
                {
                    System.out.print("Error.");
                }
            }
            else
            {
                System.out.print("Already a member.");
            }
        }
    }
    Last edited by pbrockway2; 12-14-2011 at 05:03 AM. Reason: code tags added

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

    Default Re: Writing data to an array

    You have to store values here,

    Java Code:
    else
                {
                    System.out.print("Error.");
                }
    But I cannot see that you have tried anything.

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

    Default Re: Writing data to an array

    I thought I would have to store values where ever the null position in the array is searched for, which would be within the if statement ?

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

    Default Re: Writing data to an array

    Here you are doing the validation, tight?

    Java Code:
                if (place <customerName.length && !name.equals(customerName[place]) && customerName[place].equals(null))
                {
                    name = customerName[place];
                }

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

    Default Re: Writing data to an array

    Yes that's right, in that part I'm making sure that the position is less than the array length, that the name entered by the scanner is not within the array and that the position is null, isn't that correct ?
    Then the part within the if statement writes the String imputed through the scanner into the next null position?
    Sorry I'm still quite new to this.
    Last edited by Sharoo; 12-14-2011 at 07:37 AM.

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

    Default Re: Writing data to an array

    Yes, you can do this. But the issue is every time you have to check the length and so on. If a item is missing somehow then you are in a trouble.

    Have you ever heard about ArrayList?

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

    Default Re: Writing data to an array

    I haven't heard about an ArrayList, what is it and what does it do?
    Also, is there anyway in which I could make it work the way it is currently set up ?

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

    Default Re: Writing data to an array

    Don't worry about the ArrayList for the moment. Just to take an idea from your end, in ArrayList simply you no need to worry about the size when you are working on with them. That is actually only a one benefits from a lot.

    Regarding your question, we will stick with arrays first and later you can have a look at the ArrayList.

    Your almost the complete code put into a for loop. At the moment, I don't want to say anything. Have a look at it and try to understand. If you cannot understand anything, let me know. I'll help you. :)


    Java Code:
            for(int index = 0; index < customerName.length; index++){
                if(name.equals(customerName[index])) {
                    System.out.println("Found!");
                }
                else {
                    System.out.println("Not Found!");
                }
            }

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

    Default Re: Writing data to an array

    The code has some bad practices as well. Don't worry. I'll explain them later.

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

    Default Re: Writing data to an array

    Yes I understand that
    The for statement is searching the array from position 0 and if the name inputed by the scanner is found then it stops, if not it continues to the end of the array and prints the else statement.

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

    Default Re: Writing data to an array

    Ok, now can you add your requirement into it? Give a try and see.

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

    Default Re: Writing data to an array

    By that, do you mean change the else statement so that it adds the new String to the array ?

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

    Default Re: Writing data to an array

    So I tried using the code you gave me and I tried making it work how I thought it should. When I use an name that is already within the array it works fine and I get the message Found! which means it is working. When I use a name that isn't in the array I get an error message when I try running it, java.lang.NullPointerException: null, it compiles fine however.

    Java code:

    Java Code:
    import java.util.*;
    
    public class test
    {
        private static int size = 20;
        private String customerName[];
    
        public void customerName()
        {
            customerName = new String[size];
            customerName[0] = "Joshua";
            customerName[1] = "Sandee";
            customerName[2] = "Sam";
            customerName[3] = "Laure";
            customerName[4] = "Joseph";
            int place = 0;
            String name;
            Scanner in = new Scanner(System.in);
            System.out.print("What is the customers name: ");
            name = in.nextLine();
            for(int index = 0; index < customerName.length; index++)
            {
                if(name.equals(customerName[index])) 
                {
                    System.out.println("Found!");
                    break;
                }
                else 
                {
                    if(index <customerName.length && !name.equals(customerName[index]))
                    {
                        name = customerName[index];
                    }
                }
             }
        }
    }
    I'm not sure what I'm doing wrong here.
    PS. Sorry I'm not sure how to make the code look pretty.
    Last edited by Eranga; 12-14-2011 at 09:25 AM. Reason: code tags added

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

    Default Re: Writing data to an array

    Quote Originally Posted by Sharoo View Post
    PS. Sorry I'm not sure how to make the code look pretty.
    Don't worry. Practice will make you perfect. :)

    Let see you error then.

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

    Default Re: Writing data to an array

    I was just told by a friend that name = customerName[index] should be the other way around because you're replacing the null with the name inputed with the scanner ?

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

    Default Re: Writing data to an array

    In else block, you know that you didn't find the name you entered. You have to keep track of it. Say you enter the last name of the list, Joseph. Four times you get the Not Found.

    So what you can do is simply use a boolean flag.

    The issue you have is in each not found case you are storing the value in the array. Got it?

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

    Default Re: Writing data to an array

    I understand the first part about it going through the loop five times until it finds Joseph, but I don't know what you mean by a boolean flag and I don't know what you mean by the last sentence.

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

    Default Re: Writing data to an array


    Have a look at the following code segment. I tried to keep it simple as much as possible. Hope you can understand it.


    Java Code:
    public  void customerName()
        {
            customerName = new String[size];
            customerName[0] = "Joshua";
            customerName[1] = "Sandee";
            customerName[2] = "Sam";
            customerName[3] = "Laure";
            customerName[4] = "Joseph";
            
            Scanner in = new Scanner(System.in);
            System.out.print("What is the customers name: ");
            String name = in.nextLine();
            
            // Set the flag to true, only if the name is found in the existing array
            boolean bFlag = false;
            
            for(int index = 0; index < customerName.length; index++){
                if(name.equals(customerName[index])) {
                    System.out.println("Already a member!");
                    bFlag = true;
                    break;
                }
                else {
                    // Still the flag is false. You can remove this because once we found the name in the array,
                    // we break and exit from the outer loop
                    bFlag = false;
                }
            }
            
            // If the flag is false we can add the name to array
            if(!bFlag) {
                for(int index = 0; index < customerName.length; index++){
                    if(customerName[index] == null) {
                        customerName[index] = name;
                        break;
                    }
                }
            }
            
            // Displya to verify
            for(int i = 0; i < customerName.length; i++){
                System.out.println(customerName[i]);
            }
        }

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

    Default Re: Writing data to an array

    I'm not exactly sure what you mean by a boolean flag, is that like making the method boolean instead of void ?

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

    Default Re: Writing data to an array

    No! No!

    Don't mess-up with those two. Flag is a kind of word we are using in programming, is a variable where we can hold a status of two scenarios (using true or false). Here in our example, either the name is found on the list or not.

    Method return type is something else.

Page 1 of 2 12 LastLast

Similar Threads

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