943,587 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 42687
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2008
0

Parse String

Expand Post »
Hi,

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

Java Syntax (Toggle Plain Text)
  1. String strVariable;
  2. Char charVariable;
  3.  
  4. strVariable = "Some String";
  5. charVariable = String.ParseString(strVariable);

The bottom lines yields an error...is this possible to do?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Zurompeta is offline Offline
54 posts
since Sep 2008
Nov 19th, 2008
0

Re: Parse String

Click to Expand / Collapse  Quote originally posted by Zurompeta ...
Hi,

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

Java Syntax (Toggle Plain Text)
  1. String strVariable;
  2. Char charVariable;
  3.  
  4. strVariable = "Some String";
  5. 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
Reputation Points: 919
Solved Threads: 353
Nearly a Posting Maven
stultuske is online now Online
2,473 posts
since Jan 2007
Nov 19th, 2008
0

Re: Parse String

Makes sense.

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

Works Fine:
Java Syntax (Toggle Plain Text)
  1. String strVariable = jTextField1.getText();

Error:
Java Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Zurompeta is offline Offline
54 posts
since Sep 2008
Nov 19th, 2008
0

Re: Parse String

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

what you can do, is :
Java Syntax (Toggle Plain Text)
  1. String strVariable = jTextField1.getText();
  2. Char[] allChars = strVariable.toCharArray();

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

Java Syntax (Toggle Plain Text)
  1. char firstChar = allChars[0];
  2. char secondChar = allChars[1];
  3. ...

instead of a String object
Reputation Points: 919
Solved Threads: 353
Nearly a Posting Maven
stultuske is online now Online
2,473 posts
since Jan 2007
Nov 19th, 2008
0

Re: Parse String

Sorry, I left out some important information.

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

Java Syntax (Toggle Plain Text)
  1. array[index].setVariable(variable);

So I tried what you suggested, but I'm not sure how I would implement that.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Zurompeta is offline Offline
54 posts
since Sep 2008
Nov 19th, 2008
0

Re: Parse String

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

Java Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 919
Solved Threads: 353
Nearly a Posting Maven
stultuske is online now Online
2,473 posts
since Jan 2007
Nov 19th, 2008
0

Re: Parse String

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:

Java Syntax (Toggle Plain Text)
  1. public class GuestPass {
  2.  
  3. private int guestNum;
  4. private int numLeft;
  5.  
  6. public GuestPass () {
  7. }
  8.  
  9. public int getGuestNum () {
  10. return guestNum;
  11. }
  12.  
  13. public void setGuestNum (int val) {
  14. this.guestNum = val;
  15. }
  16.  
  17. public int getNumLeft () {
  18. return numLeft;
  19. }
  20.  
  21. public void setNumLeft (int val) {
  22. this.numLeft = val;
  23. }
  24.  
  25. public void getGuestCode () {
  26. return guestCode;
  27. }
  28.  
  29. public void setGuestCode (char val) {
  30. this.guestCode = val;
  31. }
  32.  
  33. }

So to get/set from this class I setup an array:
Java Syntax (Toggle Plain Text)
  1. static GuestPass guests[] = new GuestPass[50];

To get:
Java Syntax (Toggle Plain Text)
  1. guests[index].getGuestCode();

To set:
Java Syntax (Toggle Plain Text)
  1. guests[index].setGuestCode('T');

I hope this helps
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Zurompeta is offline Offline
54 posts
since Sep 2008
Nov 19th, 2008
0

Re: Parse String

Click to Expand / Collapse  Quote originally posted by Zurompeta ...
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:
Java Syntax (Toggle Plain Text)
  1. String strVariable = jTextField1.getText();
  2. Char[] allChars = strVariable.toCharArray();
  3.  
  4. GuestPass[] allGuests = new GuestPass[strVariable.length()];
  5. for(int i = 0; i < strVariable.length(); i++){
  6. GuestPass help = new GuestPass();
  7. help.setGuestCode(allChars[i]);
  8. allGuests[i] = help;
  9. }
Reputation Points: 919
Solved Threads: 353
Nearly a Posting Maven
stultuske is online now Online
2,473 posts
since Jan 2007
Nov 19th, 2008
0

Re: Parse String

Hey that worked!

Thanks for all the help
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Zurompeta is offline Offline
54 posts
since Sep 2008
Apr 27th, 2011
0
Re: Parse String
Click to Expand / Collapse  Quote originally posted by stultuske ...
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:
Java Syntax (Toggle Plain Text)
  1. String strVariable = jTextField1.getText();
  2. Char[] allChars = strVariable.toCharArray();
  3.  
  4. GuestPass[] allGuests = new GuestPass[strVariable.length()];
  5. for(int i = 0; i < strVariable.length(); i++){
  6. GuestPass help = new GuestPass();
  7. help.setGuestCode(allChars[i]);
  8. allGuests[i] = help;
  9. }
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeffbroodwar is offline Offline
1 posts
since Apr 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: some questions regarding use of Log4j in java desktop applications
Next Thread in Java Forum Timeline: Java Media Framework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC