cheq string

how to cheq string

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 968 Views - Last Post: 20 January 2010 - 06:58 PM Rate Topic: -----

#1 vickyvvv44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 08

cheq string

Posted 20 January 2010 - 09:10 AM


String st = "abcd";
int i=0;
while(i<st.length())
{
char c = ?........(st.substring(i,i+1));
System.out.Println(c);
i++;
}



Is This A Good Question/Topic? 0
  • +

Replies To: cheq string

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: cheq string

Posted 20 January 2010 - 09:15 AM

I wouldn't use a while loop. And you don't have to use the substring() method.

String st = "abcd";

// edit the loop a bit

for (int i = 0; i < st.length(); i++)
{
    char ch = st.charAt(i); // use the charAt() method to access characters

    System.out.println(ch);
}


Hope this helps! :)

This post has been edited by Locke: 20 January 2010 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 vickyvvv44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-September 08

Re: cheq string

Posted 20 January 2010 - 09:22 AM

String  st ="condition";
int len = st.length();
int y= 0;
while(len>y)
{
String c = st.Substring(y,y+1);
char ch = c;
if(ch>='A'&& ch<='Z' || ch>='a' && ch<= 'z' )
{y++;
}
else{break;}
}
if(i==len){System.out.Println("valid");}
else{System.out.Println("unvalid ");}


Edited by Locke. Please, in the future --> :code:

This post has been edited by Locke: 20 January 2010 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: cheq string

Posted 20 January 2010 - 09:46 AM

please put [ code] [ /code] tags around your code
& Please tell us what your issue is

oh and its "check" not "cheq"

This post has been edited by SwiftStriker00: 20 January 2010 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: cheq string

Posted 20 January 2010 - 09:49 AM

You don't have a variable i. But you do have a y. :)

if (y == len) // not 'i'

Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: cheq string

Posted 20 January 2010 - 09:52 AM

String cStr = "c";
		char c = cStr.toCharArray()[0];

This post has been edited by SwiftStriker00: 20 January 2010 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: cheq string

Posted 20 January 2010 - 10:01 AM

String c = st.Substring(y,y+1);



substring is lowercased

Also, since c is a String, you cannot convert it to a char just by doing this:

String c = st.Substring(y,y+1);
char ch = c;



You could, as SwiftStriker suggested, do this:
String a = "a";
char s = a.toCharArray()[0];

Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: cheq string

Posted 20 January 2010 - 10:26 AM

Nobody suggests charAt()? Interesting.
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2696
  • View blog
  • Posts: 15,734
  • Joined: 24-September 08

Re: cheq string

Posted 20 January 2010 - 10:29 AM

They already did in his/her other 6 threads s/he made about the same topic.
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: cheq string

Posted 20 January 2010 - 10:55 AM

I know I already did in one of them.

But since we're talking about accessing individual characters, I'm surprised that didn't come up here. It would involve another variable to continuously go through the string, but meh.
Was This Post Helpful? 0
  • +
  • -

#11 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: cheq string

Posted 20 January 2010 - 11:05 AM

charAt() is too obvious of an answer :P
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2733
  • View blog
  • Posts: 11,509
  • Joined: 20-September 08

Re: cheq string

Posted 20 January 2010 - 11:28 AM

boolean valid = st.matches("[A-Za-z]+");


Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: cheq string

Posted 20 January 2010 - 04:47 PM

That's the easy one, but not for beginners. :D
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: cheq string

Posted 20 January 2010 - 06:34 PM

The String class has a method to return an array of char[] from a String

char[] digit = string.toCharArray();
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: cheq string

Posted 20 January 2010 - 06:35 PM

Also, please post your question in the body of your post, not in the title where it gets truncated.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2