comparing String.equals to space char?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: comparing String.equals to space char?

  1. #1
    Join Date
    Oct 2002
    Posts
    25

    comparing String.equals to space char?

    Writing code that will take a Symbol and expand it into a sentence using basic grammar rules. I have included a copy of my code in text format (it will not compile because Telnet ate several lines) but it should give you an idea where I am going with the algorithm.

    The bug that is sticking me is in my Extract_Category method. Probably in these three lines:

    if(nextChar.equals(" ")

    int end = i;
    break;

    The method accepts the String NAME VP and should iterate through the string and extract the substring NAME. However it iterates right through the space character and returns the whole string "NAME VP". Why would it not recognize the space character?

    Here is most of my code
    Attached Files Attached Files
    "I hope that one day I will be able to answer someone elses questions!"

  2. #2
    Join Date
    Oct 2002
    Posts
    25
    p.s. The nextChar variable is a String object converted from char to String earlier in the code.

    Thanks for any help.

    John
    "I hope that one day I will be able to answer someone elses questions!"

  3. #3
    Join Date
    Sep 2002
    Posts
    16
    You could try something like this:
    Code:
    if (string.charAt(counter) == ' ')
    Jayceepoo

  4. #4
    Join Date
    Oct 2002
    Posts
    25

    Unhappy Continuation of this problem

    OK, I have a String object "john" that I am passing to a function Simple_String_Scan. Simple_String_Scan is supposed to pull off each character from the String and check to see if it is uppercase. If it finds an uppercase character in the String it should return False. Right now it returns out of bounds exception...


    public static boolean Simple_String_Scan(String s){

    boolean containsCategory = false;

    for(int i = 0; i<= s.length(); i++){

    String nextChar = String.valueOf(s.charAt(i));

    if(nextChar.toUpperCase()==nextChar){

    containsCategory = true;

    break;

    }//End of if statement



    return containsCategory;

    }//End of Simple_String_Scan

    Below I have posted the results of six different String inputs to the function, Case 5 and 6 should have returned False.

    BEGIN LOOP 1 String ="S"
    The value returned from Simple_String_Scan for case 1 was true
    BEGIN LOOP 2 String ="NAME VP"
    The value returned from Simple_String_Scan for case 2 was true
    BEGIN LOOP 3 String ="john V NP"
    The value returned from Simple_String_Scan for case 3 was true
    BEGIN LOOP 4 String ="john V a good teacher"
    The value returned from Simple_String_Scan for case 4 was true
    BEGIN LOOP 5 String ="john is a good teacher"
    The value returned from Simple_String_Scan for case 5 was true
    BEGIN LOOP 6 String ="john"
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
    ex out of range: 4
    at java.lang.String.charAt(String.java:455)
    at Generator.Simple_String_Scan(Generator.java:57)
    at GenTest.main(GenTest.java:47)

    Any help greatly appreciated. Thanks
    "I hope that one day I will be able to answer someone elses questions!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center