Hi,

Basically, I'm trying to parse a string into a char for submitting into an array, this is what I have so far:

String strVariable;
Char charVariable;

strVariable = "Some String";
charVariable = String.ParseString(strVariable);

The bottom lines yields an error...is this possible to do?

Recommended Answers

All 12 Replies

Hi,

Basically, I'm trying to parse a string into a char for submitting into an array, this is what I have so far:

String strVariable;
Char charVariable;

strVariable = "Some String";
charVariable = String.ParseString(strVariable);

The bottom lines yields an error...is this possible to do?

no
first of all, it's no use to parse a String to a String, secondly, you can not put a String object in a char

Makes sense.

Well, to help with the situation, I'm trying to get text from a text field.

Works Fine:

String strVariable = jTextField1.getText();

Error:

Char charVariable = jTextField1.getText();

I'm trying to do this because I'm trying to input data into a portion of an array that requires the data to be of a char data type.

it makes sense that you get this error. the getText() method of the JTextFields return a String object.

what you can do, is :

String strVariable = jTextField1.getText(); 
Char[] allChars = strVariable.toCharArray();

now you have the same data, but as an array of chars, which you can address seperately

char firstChar = allChars[0];
char secondChar = allChars[1];
...

instead of a String object

Sorry, I left out some important information.

I have classes set up and the data is being put into classes in the following format:

array[index].setVariable(variable);

So I tried what you suggested, but I'm not sure how I would implement that.

since I have no idea what kind of object array is an array of, or what kind of type variable has ... how do you think I can help you with it?

if the point is to have this variable as an element in the array though, you're going about it the wrong way. if so, you should do

array[index] = variable;

if you want a variable within the element of array to be set to variable, you'll have to be just a tad more specific about what you're doing.

Alright, I'll see what I can do about being more specific, I'm finding it a bit hard to explain.

Hmm, well I had to make a UML Class Diagram where each class in the diagram had Attributes in it, and with each attribute is a "get" and a "set". The gets and sets are accessible through arrays. Here's the code from a class that was compiled from the diagram:

public class GuestPass {
 
    private int guestNum;
    private int numLeft;

    public GuestPass () {
    }

    public int getGuestNum () {
        return guestNum;
    }

    public void setGuestNum (int val) {
        this.guestNum = val;
    }

    public int getNumLeft () {
        return numLeft;
    }

    public void setNumLeft (int val) {
        this.numLeft = val;
    }

    public void getGuestCode () {
        return guestCode;
    }

    public void setGuestCode (char val) {
        this.guestCode = val;
    }

}

So to get/set from this class I setup an array:

static GuestPass guests[] = new GuestPass[50];

To get:

guests[index].getGuestCode();

To set:

guests[index].setGuestCode('T');

I hope this helps

The gets and sets are accessible through arrays.

the getters and setters are accessible through the instances of GuestPass that you've put in an Array.

now, since I still don't know what exactly it is supposed to do, I'm taking a wild guess here:

String strVariable = jTextField1.getText();
      Char[] allChars = strVariable.toCharArray();

      GuestPass[] allGuests = new GuestPass[strVariable.length()];
      for(int i = 0; i < strVariable.length(); i++){
          GuestPass help = new GuestPass();
          help.setGuestCode(allChars[i]);
          allGuests[i] = help;
      }

Hey that worked!

Thanks for all the help :)

the getters and setters are accessible through the instances of GuestPass that you've put in an Array.

now, since I still don't know what exactly it is supposed to do, I'm taking a wild guess here:

String strVariable = jTextField1.getText();
      Char[] allChars = strVariable.toCharArray();

      GuestPass[] allGuests = new GuestPass[strVariable.length()];
      for(int i = 0; i < strVariable.length(); i++){
          GuestPass help = new GuestPass();
          help.setGuestCode(allChars[i]);
          allGuests[i] = help;
      }

Hi,

can you post the code on how this should work? I'm afraid the this.guestCode method doesn't exist and I'm not sure about the idea of what we would want to do with get and set. thanks in advance.

You can parse a string using the split method string

inputstr="a,b";
String patternstr="[,]";
String [] field=inputstr.split(patternstr);

@siviwe
You do realise that this was marked as "solved" in 2008, don't you? It's 2011 in this part of the world...

lol,sorry for that it's just im new to this and i was just testing it out to see if it really works and it just so happened that your thread was the unfortunate subject.

commented: This post makes no sense at all. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.