Java noob

  • Thread starter Thread starter Kua
  • Start date Start date

Kua

Kua

Associate
Joined
21 Jul 2008
Posts
512
Location
Lancaster
Hullo! I'm a first year CS student at Lancaster and I'm really struggling with the Java module. I'm hoping it'll all click into place soon. Not sure how appropriate it is to copy-pasta the whole question.

But I have to write an encode class that takes a String as its input parameter and returns an array of int values, based on a message the user inputs. Each int will be the ASCII value for each character in the original string.

We have to cast each char as an int, but the original message is a String. So I really have no clue what to do.

I understand the concept of arrays but **** me if I can use them!

I've probably not explained that too well, but like I say I don't know whether its kosher to show the whole question.
 
I have done exactly this in c++ if that helps at all? I am not sure I can post my code here though as it is for a uni project.

May be explain the concepts if you can... Do I need to cast the String as an array of Chars? If that's even possible.
 
And to get the ascii code you would do

int ascii_code = 0;

ascii_code = int(array[0]);

to get the first letter. But set it up as a loop to an array instead.

Cheers man, Feel as though I've got something to work off now. (That normally means I'm about to get stuck again. But we can always hope)
 
Yikes this is devilishly difficult.

"In your Encode class, write a method that takes a String as an input parameter and returns an array of int values, based on the contents of the original message. Each element in the array will be the ASCII value of the relevant character of the message string."

The input has to be a variable that's in my main program, so I don't know whether I can simply us the name of the variable in the parameter for the constructor method I'm creating....

Here's the code I have in the Encode class:

Code:
public class Encode {

public encode(message){
  
    char myArray[] = message.toCharArray();
    
    int asciiCode;
    
    for(int j=0; j<myArray.length; j++) {
    asciiCode = int(myArray[j]);
    return myArray[j]
    }
    
}

(Let's hope that's valid markup!)

Code:
public QuestionDialog qDialog = new QuestionDialog ("Enter your message");

void setup()
{

      String message = qDialog.getAnswer();
      println(message);
      
            
      
      for (int i=0; i<myArray.length; i++)
      println(array[i]);
      
}

And thats the code in the main program...

Sorry. My heads swimming again now. I seem to keep losing track of what I'm doing. Its not like reading a book, where you just continue where you left off. Its like spinning dozens of plates. I just can't keep em all spinning!

I'm sure that metaphor makes no sense whatsoever. But this does get easier, right?
 
Last edited:
So it looks like this?

Code:
public encode(String){

//stuff

}

And the code you posted above goes inside the main program?

I've named it encode here, but you've named it StringToIntArray.
 
Thanks Rob. I've got it working but we're using Processing and it didn't like this line of code:

Code:
for (int i : ASCIIMessage) {
   System.out.println(i);
}

So I used this instead:

Code:
for (int i = 0; i < ASCIIMessage.length; i++) {
   println(ASCIIMessage[i]);
}

Still feeling a bit all at sea with it, but I'll keep plugging away :p
 
Back
Top Bottom