fatmarik
Member
+23|6578|Anywhere i am needed
i need someone's help for a java program.

O.K. Here is the question I was given:

Given the three parameters: 1) an array of chars named letters, 2) a beginning letter named beg, 3) and an ending letter named end, write a method arrayOfLetters that will return a new array of chars that contains all the characters in the array letters that are between beg and end (inclusively) found in letters.

Here is what I have so far:

public char[] arrayOfLetters (char[] letters, char beg, char end)
{
char[] temp;
for (int x=0; x<letters.length; x++)


}

I just need someone to show me how to finish the rest, you can add me on xfire:  fatmarik  .
ThaReaper
Banned
+410|6636
I'm in computer science, but we haven't learned about that yet. =\
Mr.Dooomed
Find your center.
+752|6324

that sounds confusing, maybe I should drop my java course
Nature is a powerful force. Those who seek to subdue nature, never do so permanently.
Ayumiz
J-10 whore
+103|6730|Singapore
Hohohoh, dont ask me about it, i failed my fucking JAVA.
Vilham
Say wat!?
+580|6762|UK
lol easy. Ill give you a full answer tomorrow. Ive gotta sleep now...
Yellowman03
Once Again, We Meet at Last
+108|6231|Texas
public char[] arrayOfLetters (char[] letters, char beg, char end)
{
ArrayList<char> temp = new ArrayList<char>() ;
char[] returnArray;
int count = 0;
for (int x=0; x<letters.length; x++)
{
  if(char[x].compareTo(letters[beg]) > 0 && char[x].comparTo(letters[end]) < 0){
  temp.add(char[x]);
  count++;
}
returnArray = new char[count];
return temp.toArray(returnArray[]);
}

i think that will work...not sure tho
.Sup
be nice
+2,646|6449|The Twilight Zone

Ayumiz wrote:

Hohohoh, dont ask me about it, i failed my fucking JAVA.
https://www.shrani.si/f/3H/7h/45GTw71U/untitled-1.png
Scorpion0x17
can detect anyone's visible post count...
+691|6762|Cambridge (UK)
OP: First up, it depends what is meant by "all the characters in the array letters that are between beg and end (inclusively)"...

I'm going to assume it means, say we have letters={ 'a', 'd', 'f', 'b', 'h', 'o', 'j' }, and you call arrayOfLetters(letters, 'd', 'h'), then it should return the array { 'd', 'f', 'b', 'h' }...

And, sorry, my Java's not up to a good enough standard to write correct code, but here's some psuedo-code:

Code:

public char[] arrayOfLetters (char[] letters, char beg, char end)
{
 char[] temp;
 bool flag;
 int a=0;

 for (int x=0; x<letters.length; x++)
 {
  if(letters[x]==beg) flag=true;
  if(flag) temp[a++]=letters[x];
  if(letters[x]==end) flag=false;
 }
}
If on the other hand, it means between as in alphabetically between (e.g. 'b' is between 'a' and 'c'), Yellowmans code looks about right, except it should be letters[x] not char[x] in the main loop...

Last edited by Scorpion0x17 (2008-02-25 23:23:37)

Vilham
Say wat!?
+580|6762|UK

Scorpion0x17 wrote:

OP: First up, it depends what is meant by "all the characters in the array letters that are between beg and end (inclusively)"...

I'm going to assume it means, say we have letters={ 'a', 'd', 'f', 'b', 'h', 'o', 'j' }, and you call arrayOfLetters(letters, 'd', 'h'), then it should return the array { 'd', 'f', 'b', 'h' }...

And, sorry, my Java's not up to a good enough standard to write correct code, but here's some psuedo-code:

Code:

public char[] arrayOfLetters (char[] letters, char beg, char end)
{
 char[] temp;
 bool flag;
 int a=0;

 for (int x=0; x<letters.length; x++)
 {
  if(letters[x]==beg) flag=true;
  if(flag) temp[a++]=letters[x];
  if(letters[x]==end) flag=false;
 }
}
If on the other hand, it means between as in alphabetically between (e.g. 'b' is between 'a' and 'c'), Yellowmans code looks about right, except it should be letters[x] not char[x] in the main loop...
Yup scorp thats how I thought it would be done too. That line you point out is one ambiguous son of a bitch.

Code:

public char[] arrayOfLetters (char[] letters, char beg, char end)
{
 char[] temp;
 int a=0;
boolean flag = false;

 for (int x=0; x<letters.length; x++)
 {
    if(letters[x]==beg){ 
      flag = true;
    }
    if(flag){
      temp[a]=letters[x];
      a++
    }
    if(letters[x]==end){
      break;
    }
 }
}
Thats scorps pseudo code but slightly corrected. As I assume you want all letters between those two points.

Last edited by Vilham (2008-02-26 09:52:45)

ghettoperson
Member
+1,943|6645

I hate Java so much...
jsnipy
...
+3,276|6518|...

Edit:  // assuming end needs ot be included

Code:

    // Given the three parameters: 1) an array of chars named letters, 2) a beginning 
    // letter named beg, 3) and an ending letter named end, write a method arrayOfLetters that
    // will return a new array of chars that contains all the characters in the array letters 
    // that are between beg and end (inclusively) found in letters.
    public char[] arrayOfLetters(char[] letters, char beg, char end) 
    {
        // TODO: null check for paramters
        
        ArrayList<Character> working = new ArrayList<Character>();
         boolean begHit = false;
                
        for(char c:letters)
        {
            if(begHit || c == beg )
            {
                begHit = true;
                if(c == end)
                {
                    working.add(c); // assuming end needs ot be included
                                                                    break;
                }
                working.add(c);
            }
        }
        
        // TODO: correct syntax returning working.toArray(); 
    }

Last edited by jsnipy (2008-02-26 10:07:57)

Vilham
Say wat!?
+580|6762|UK
Just realised mine has a flag missing
Scorpion0x17
can detect anyone's visible post count...
+691|6762|Cambridge (UK)

Vilham wrote:

Just realised mine has a flag missing
You and I both forgot the need to return the temp array as well...

Code:

public char[] arrayOfLetters (char[] letters, char beg, char end)
{
 char[] temp;
 int a=0;
 boolean flag = false;

 for (int x=0; x<letters.length; x++)
 {
    if(letters[x]==beg){ 
      flag = true;
    }
    if(flag){
      temp[a]=letters[x];
      a++
    }
    if(letters[x]==end){
      break;
    }
 }
 return temp;
}
There, that should be about right, I think...

Last edited by Scorpion0x17 (2008-02-26 23:10:27)

Vilham
Say wat!?
+580|6762|UK
lol good point. I forgot this was a function and not a main. Normally when your learning java you just do everything in the main till you have the basics covered before learning how to do return statements and actual different types of functions.
jsnipy
...
+3,276|6518|...

Vilham wrote:

lol good point. I forgot this was a function and not a main. Normally when your learning java you just do everything in the main till you have the basics covered before learning how to do return statements and actual different types of functions.
Does your array automticlly grow
Vilham
Say wat!?
+580|6762|UK
meh just change array to vector then it does.
fatmarik
Member
+23|6578|Anywhere i am needed
wow thanks guys, you helped me a lot, ill give +1 to everyone tomorrow
FredFLQ
S0tp R4p1nG u5!!1one
+47|6527|Donnacona, Quebec
I'd love to help you, but I know jack shit about JAVA =p

Board footer

Privacy Policy - © 2024 Jeff Minard