Results 1 to 11 of 11
  1. #1
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default would this compile?

    Would this work?

    proposed algorithym:

    1. takes array: eg {A,B,C,!,!,}

    2. if array contains letters from string alphabet, then add the same letter to
    second array called arrayExtra

    3 if not from alphabet then add space to array.

    4. change elements of array to elements of arrayExtra.

    Java Code:
    public void strip(char[] array)
    {
    	string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	char[] arrayExtra = new char[array.length-1]
    	
    	for(int i = 0; i < arrayextra; i ++)
    	{
    		if(array[i].equalsAlphabet.charAt(i))
    		{
    			arrayExtra[i] = array[i];
    		}
    		else
    		{
    			arrayExtra[i] = ' ';
    		}
    	}
    	
    	array = arrayExtra;
    }
    Last edited by stringkilla; 10-24-2010 at 11:52 AM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    >>would this compile?

    No! Other questions?

  3. #3
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    care to elaborate and suggest changes to complete the algorithm?

  4. #4
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    anyone else?

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    I see so many things wrong I don't know where to start. First what do you mean when you say:

    Java Code:
    i < arrayextra

    It makes no sense to compare an array to an integer. Maybe you were thinking about the length? Also, there is a syntax error here.

    Second, you will need nested for loops to accomplish your task because this code does not look through all possible matches. In fact I think it only checks to see if array is equal to alphabet. Let me try to illustrate your loop:

    ABCDEFGHI...XYZ <----- Alphabet String
    JOSHDGREEN <----- Array Passed In

    If J is equal to A, which it isn't so a blank space is entered.
    If O is equal to B, which it isn't so a blank space is entered.
    If S is equal to C, which it isn't so a blank space is entered.
    etc...
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    public void strip(char[] array)
    {
    	char[] Alphabet = {A,B,C,D,E,F,G,H,I....}
    	char[] arrayExtra = new char[array.length-1]
    	
    	for(int i = 0; i < array.length; i ++)
    	{
    		charCurrent = array[i];
    
    		for(int i = 0; i < alphabet.length; i ++)
    		{
    			if(charCurrent==Alphabet[i])
    			{
    				arrayExtra[i] = charCurrent;
    			}
    			else
    			{
    				arrayExtra[i] = ' ';
    			}
    		}
    	}
    	
    	array = arrayExtra;
    }
    What about this?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by stringkilla View Post
    What about this?
    Do you have to pay for your javac compiler to do its job?

    kind regards,

    Jos

  8. #8
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    
    public class practest5
     {
    	public static void main(String[] args)
    	{
    
    
    		char charCurrent='0';
    		char[] array = {'J', 'A', 'S'};
    		char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    		char[] arrayExtra = new char[array.length-1];
    
    		for(int i = 0; i < array.length; i ++)
    		{
    			charCurrent = array[i];
    
    			for(int j = 0; j < Alphabet.length; j ++)
    			{
    				if(charCurrent==Alphabet[j])
    				{
    					arrayExtra[i] = charCurrent;
    				}
    				else
    				{
    					arrayExtra[i] = ' ';
    				}
    			}
    		}
    
    	array = arrayExtra;
    
    	for(int i = 0; i < array.length; i++)
    		{
    			System.out.println(array[i]);
    		}
    
    	}
    }
    this compiles but i get the output:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at practest5.main(practest5.java:28)

    which is:

    Java Code:
    arrayExtra[i] = ' ';
    any ideas?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by stringkilla View Post
    any ideas?
    Yup, according to your problem description the additional array contains the same number of characters as the original one (wich non-letter characters replaced by spaces) so it is beyond me why you create a shorter array than the original one ...

    kind regards,

    Jos

  10. #10
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    so how could this be fixed?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by stringkilla View Post
    so how could this be fixed?
    Don't make that other array shorter than the original one. I am not going to spoonfeed it to you and I'm not going to do your homework for you; you should think for yourself and at least experiment a little. I already gave you the clue twice, now it's your turn.

    kind regards,

    Jos

Similar Threads

  1. Can't Compile
    By sidk47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-15-2010, 04:43 PM
  2. Help with compile
    By mr_anderson in forum NetBeans
    Replies: 7
    Last Post: 06-10-2010, 04:03 AM
  3. .java won't compile
    By paul21 in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 06:58 AM
  4. doesn't compile?!
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 06-14-2008, 05:42 PM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 10:13 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
  •