Results 1 to 18 of 18
  1. #1
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default help to understand this problem clear

    a problem i get into a book entitled Absolute JAVA.
    Write a static method called deleteRepeats that hasa partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Because a partially filled array requires two arguments, the method should actually have two parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted, the remaining letters are moved up one position to fill in the gap. This creates empty positions at the end of the array so that the less of the array is used. Because the formal parameter is partially filled array, a second formal parameter cannot be changed by a JAVA method, so have the method return the new value for this parameter. For example, consider the following code:
    Java Code:
    char a[10];
    a[0] = 'a';
    a[1] = 'b';
    a[2] = 'a';
    a[3] = 'c';
    int size=4;
    size = deleteRepeats(a, size);
    After this code is executed, the value of a[0] is 'a', the value os [1] is 'b'. the value of a[2] is 'c', and the value of size is 3.(The value of a[3] is no longer of any concern, because the partially filled array no longer uses this indexed variable.)
    here is the problem..
    i cant understand it more clearly..
    is there anyone who could summarize this in order for me to understand it clearly?
    i dont said that you write a program for me..
    i just said is to help me understand this problem clearly..
    help

  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

    Something like this,

    You have an array of char types. You can added up to 10 elements there. Another variable is also there to handle the number of filed index in the array, type of int. You cannot have duplicate letters.


  3. #3
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    is this work:
    Java Code:
    char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
    int x;
    x as the variable that handle the number of filled index in the array..
    is that what you trying to say?

  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

  5. #5
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    i came up w/ this code:
    Java Code:
    public class Exercise
    {
    	public static void main(String[]args)
    	{
    	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
    	int x;
    	for(x=i;x<letter.length;x++)
    	System.out.println(x);
    	}
    }
    i want to print those values in my array using the int x.
    but it doesnt work..
    what was the problem?
    Last edited by beginner21; 12-01-2008 at 12:50 PM. Reason: tag

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    What is that value i?Where did you get the variable i?
    Last edited by serjant; 12-01-2008 at 01:12 PM.

  7. #7
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    that is my problem..
    how can i print the values in the array using the int x?
    as you can see in my previous post, the code doesn't work..
    i want to print the values inside the array but the i has no value..
    what should i do?

  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

  9. #9
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    here is the error message i got:
    Java Code:
    Num4.java:7: cannot resolve symbol
    symbol : variable i
    location: class Num4
           for(x=i;x<letter.length;x++)
                 ^
    i think the error said that where i initialized variable i?
    is there another way how to print the values in the array?
    Last edited by beginner21; 12-01-2008 at 01:27 PM. Reason: spacing

  10. #10
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    it should be
    Java Code:
    for(x=0;x<letter.length;x++)

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmm...

    • First of all, it is good practice to put curly brackets "{}" after for & if statements, even if they're one liners.
    • Next, get rid of the i variable.
    • Initialize the x variable in you for statement as you would in any other for statement.
    • If you want to print the array elements, you can use the x variable:

    Java Code:
    System.out.println("letter[" + x + "] = " + letter[x]);
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    it should be
    Java Code:
    for(x=0;x<letter.length;x++)
    it wont work,
    the output in that code display 0-9..
    0123456789
    because you declare the x as 0 not the index of the array...
    Last edited by beginner21; 12-01-2008 at 01:52 PM. Reason: tag

  13. #13
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    tnx CSJLMAN..
    your code works
    my modified code is this:
    Java Code:
    public class Exercise
    {
    	public static void main(String[]args)
    	{
    	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
    	int x;
    	for(x=0;x<letter.length;x++)
    	System.out.println("letter[" + x + "] = " + letter[x]);
    	}
    }
    what was the next based on the problem?
    i cant understand what should be next after filling the index of array.
    is it im going to do now the method deleteRepeats();?

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Let's see if you understood...

    Before you can go to the next step (methods), you have to understand what you just did.

    Questions:
    • Do you know why my statement in my last post worked for you?
    • Serjant's code was correct, he was correcting an error you had in your for statement.
    • Do you completely understand everything that in the the println statement?


    Statement: you didn't follow my suggestion about using the curly brackets "{}" for the "for" statement.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    sorry CJSLMAN
    ok here is my modify code:
    Java Code:
    public class Exercise
    {
    	public static void main(String[]args)
    	{
    	char[] letter = {'a','b','c','d','e','f','g','h','i','j'};
    	int x;
    	for(x=0;x<letter.length;x++)
    	{
    	System.out.println("letter[" + x + "] = " + letter[x]);
    	}
    	}
    }
    sorry again,
    is that what you're trying to say?
    it works also, is the method is the next?

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Did you understand?

    I just want to be sure you understand how yoiur code is working.
    • Did you understand what changed in you code to make it working that way you wanted it to? Do NOT be satisfied that it is working without understanding why.
    • Yes, creating a method is next. Here's some help:

    Defining Methods (The Java Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    of course i understand it..
    x is now the index of my array,
    print new line after every execution of the loop because the loop will continuously executing until the condition satisfies,
    is it right?
    for the method,i cant understand what should i write inside deleteRepeats();?
    and where did this method return?

  18. #18
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Yes... that is the function of the println... I'm more interested if you understand what's in the println.
    Anyway....
    Your original question was to understand the book assignment:

    • Original array has the following elements {'a','b','a','c'}.
    • delete the repeated letters/elements in the array
    • move the remaining elements to fill the empty space left by the deleted element
    • return the new size of the array (the number of remaning elements
    • Example:

    • Original array = {'a','b','a','c'}
    • Array after deleting repeated elements = {'a','b', ,'c'} or { ,'b','a','c'}
    • Array after moving elements = {'a','b','c', ,}
    • return size = 3

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. clear cache
    By Jadellll in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 09:27 AM
  2. Please clear my doubt...............
    By vinaytvijayan in forum Advanced Java
    Replies: 0
    Last Post: 12-26-2007, 06:23 PM
  3. how to clear the data of an object
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:50 PM
  4. Help, someone clear up Interfaces for me
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:26 AM
  5. I want to do is clear the console
    By paul in forum Advanced Java
    Replies: 7
    Last Post: 08-03-2007, 06:54 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
  •