Parse String

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 46
Reputation: Zurompeta is an unknown quantity at this point 
Solved Threads: 0
Zurompeta Zurompeta is offline Offline
Light Poster

Parse String

 
0
  #1
Nov 19th, 2008
Hi,

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

  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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Parse String

 
0
  #2
Nov 19th, 2008
Originally Posted by Zurompeta View 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:

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: Zurompeta is an unknown quantity at this point 
Solved Threads: 0
Zurompeta Zurompeta is offline Offline
Light Poster

Re: Parse String

 
0
  #3
Nov 19th, 2008
Makes sense.

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

Works Fine:
  1. String strVariable = jTextField1.getText();

Error:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Parse String

 
0
  #4
Nov 19th, 2008
it makes sense that you get this error. the getText() method of the JTextFields return a String object.

what you can do, is :
  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

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

instead of a String object
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: Zurompeta is an unknown quantity at this point 
Solved Threads: 0
Zurompeta Zurompeta is offline Offline
Light Poster

Re: Parse String

 
0
  #5
Nov 19th, 2008
Sorry, I left out some important information.

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

  1. array[index].setVariable(variable);

So I tried what you suggested, but I'm not sure how I would implement that.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Parse String

 
0
  #6
Nov 19th, 2008
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: Zurompeta is an unknown quantity at this point 
Solved Threads: 0
Zurompeta Zurompeta is offline Offline
Light Poster

Re: Parse String

 
0
  #7
Nov 19th, 2008
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:

  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:
  1. static GuestPass guests[] = new GuestPass[50];

To get:
  1. guests[index].getGuestCode();

To set:
  1. guests[index].setGuestCode('T');

I hope this helps
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Parse String

 
0
  #8
Nov 19th, 2008
Originally Posted by Zurompeta View Post
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 46
Reputation: Zurompeta is an unknown quantity at this point 
Solved Threads: 0
Zurompeta Zurompeta is offline Offline
Light Poster

Re: Parse String

 
0
  #9
Nov 19th, 2008
Hey that worked!

Thanks for all the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC