943,102 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 617
  • Java RSS
Feb 8th, 2010
0

Java code for loop

Expand Post »
Java Syntax (Toggle Plain Text)
  1. // need help with this how do i get the for loop to print out just
  2. //the alphabetic words
  3.  
  4. {
  5. public static void main(String[] args)
  6.  
  7. {
  8. String string ;
  9. int index ;
  10. String sentence;
  11. String newSentence;
  12. String word;
  13. int startPos;
  14. int spacePos;
  15. int wordCount;
  16. boolean lastWord;
  17. boolean alphaWord;
  18. char letter;
  19.  
  20. System.out.println("Enter a string");
  21. sentence = EasyIn.getString();
  22.  
  23. for(index = sentence.length()-1 ;index >= 0 ; index--)
  24. {
  25. System.out.print(" "+ sentence.charAt(index) );
  26. }
  27.  
  28. startPos = 0;
  29. wordCount = 0;
  30. lastWord = false;
  31. newSentence = "";
  32.  
  33. do
  34. {
  35.  
  36.  
  37. spacePos = sentence.indexOf(" ", startPos);
  38. if (spacePos == -1)
  39. {
  40. lastWord = true;
  41. word = sentence.substring(startPos);
  42. }
  43. else
  44. {
  45. lastWord = false;
  46. word = sentence.substring(startPos, spacePos);
  47. }
  48.  
  49.  
  50. index = 0;
  51. alphaWord = true;
  52. while ((index <= word.length()-1) && (alphaWord == true))
  53. {
  54. letter = word.charAt(index);
  55. if ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z'))
  56. index++;
  57. else
  58. alphaWord = false;
  59. }
  60.  
  61.  
  62.  
  63. if (alphaWord == true)
  64. {
  65. newSentence = newSentence + word + " ";
  66. wordCount++;
  67. }
  68.  
  69.  
  70.  
  71. startPos = spacePos + 1;
  72.  
  73. } while (lastWord != true);
  74.  
  75. System.out.println();
  76. System.out.println(newSentence);
  77. System.out.println("The number of alphabetic words is: " + wordCount);
  78.  
  79. }
  80.  
  81.  
  82. }
Last edited by adatapost; Feb 8th, 2010 at 6:08 am. Reason: Added [code] tags. Encase your code in: [code] and [/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RavetodaGrave is offline Offline
3 posts
since Feb 2010
Feb 8th, 2010
0
Re: Java code for loop
I'm kind off missing a question in your post.
Reputation Points: 899
Solved Threads: 347
Nearly a Posting Maven
stultuske is offline Offline
2,428 posts
since Jan 2007
Feb 8th, 2010
0
Re: Java code for loop
Use regex to check whether a string is made up of alphanumeric characters or only letters then print out the ones you need.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

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.
Message:
Previous Thread in Java Forum Timeline: java features
Next Thread in Java Forum Timeline: Can you give me the methods for the start of this program that will??





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


Follow us on Twitter


© 2011 DaniWeb® LLC