12 Replies - 761 Views - Last Post: 25 April 2009 - 11:47 AM Rate Topic: -----

#1 constden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 09

Need help with for loop

Post icon  Posted 25 April 2009 - 09:46 AM

I'm having trouble getting my program to do what I want it to do.
This is the part of my code I need help with:

for (x = 19; x >= 0; x--){
	if (words[x] != null) 
	reverse = words[x] + " ";
	}
   reverse = reverse.substring(0,1).toUpperCase() + reverse.substring(1);
   System.out.print(reverse + ".");   



The last reverse code is trying to turn the lowercase first character from the original reverse code into an uppercase character and then add the remaining string to it. (I don't know if my explanation makes any sense :/)
The problem is that it doesn't print out everything I need it to, and does not capitalize the first letter of the sentence, but the first letter of the last word of the sentence. :(

My code works just fine, but it does not have the capitalized first letter, or a period at the end of the sentence if I just ask it to do this:
for (x = 19; x >= 0; x--){
	if (words[x] != null) 
	reverse = words[x] + " ";
   System.out.print(reverse + "");
   }   



Do you guys know what I could do to fix my code?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with for loop

#2 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Need help with for loop

Posted 25 April 2009 - 10:28 AM

Welcome to DIC, constden! :) (And way to post in code tags…you get extra brownie points! :P)

What are the x = 19 and x-- bits? Why is it counting down? Is it running through the sentence backwards? :blink:

Edit: Could you also give the output you do get?

This post has been edited by SayMoi: 25 April 2009 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 PeanutButter  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 28-March 09

Re: Need help with for loop

Posted 25 April 2009 - 10:35 AM

Well it should be reversing a sentence, so why not go from back to front. But I think that x = words.length would be more apropriate since it would make the loop work for arrays of any length.
Was This Post Helpful? 0
  • +
  • -

#4 constden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 09

Re: Need help with for loop

Posted 25 April 2009 - 10:48 AM

:)
Yes, I'm trying to reverse a sentence.
We were asked to write a loop that would continue until there were 20 words stored, or there were no more words to read, whichever comes first. That is why I had to make it start from 19.
Do you know how I could make the printed out sentence to have the first letter capitalized and to end with a period.
This is my whole code if it helps:
import java.util.Scanner;
	public class RR{   
	   public static void main (String[] args) {	  
		 //  Declare all variables to be used and request sentence from user.
		 int x;
		 String [] words;
		 String input, reverse = "";
		 Scanner scan1 = new Scanner(System.in);
		 System.out.println("Type a sentence");
		 input = scan1.nextLine();
		 input = input.toLowerCase();
		 input = input.replace("." , " ");
		 Scanner scan2 = new Scanner(input);
		 words = new String[20];
  
		// Store the words of the sentence in a String array.
		 for (x = 0; x < 20; x++) {
			if (scan2.hasNext()) 
			{
			   words[x] = scan2.next();
			}}
		   
		 // Print out the sentence in reverse order.
		 for (x = 19; x >= 0; x--){
			if (words[x] != null) 
			 reverse = " " + words[x];
	System.out.print(reverse);
		 }
		 
		// Print out sententence with capitalized first letter and period at the end
		reverse = reverse.substring(1,2).toUpperCase() + reverse.substring(2);
		 System.out.print("\n" + reverse + "."); 

  } // End of method main.
   
   } // End of class RR.


This post has been edited by constden: 25 April 2009 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:07 AM

Ok, here's your main problem. The "for" loop goes through, sets "reverse" to the next word of the sentence, and prints it out. But the problem is, when you get to the end "reverse" only contains the last word! Which means that's the only one capitalized. So instead of setting "reverse" equal to the next word, ADD each word to a String and output the whole thing OUTSIDE the "for" loop. Something like this:

String str = "";
for (x = 19; x >= 0; x--){
	if (words[x] != null) 
		str += words[x] + " ";
}
System.out.print(str);


Then try formatting "str". Best of luck!

This post has been edited by SayMoi: 25 April 2009 - 11:09 AM

Was This Post Helpful? 1
  • +
  • -

#6 constden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:18 AM

Thank you sooo much SayMoi!! Really, you have no idea how thankful I am.
I've been trying to figure this out for hours, no lie.
I was getting desperate.
If I had only known that I needed a "+" in front of that "=" I would have saved myself a headache.
You made my day.
Thank you! :D
Was This Post Helpful? 0
  • +
  • -

#7 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:28 AM

:) No problem! So glad I could help! Feel free to post questions any time…there's ALWAYS someone here who wants to help. And you should go introduce yourself as well! (Up in the top left corner…)
Was This Post Helpful? 0
  • +
  • -

#8 PeanutButter  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 28-March 09

Re: Need help with for loop

Posted 25 April 2009 - 11:29 AM

Well his reverse string was already meant to do what your str does.
I changed that for loop so it only adds spaces if it isn't the last word:
for (x = 19; x >= 0; x--){
	if (words[x] != null) {
		reverse += words[x];
		if (x != 0)
			reverse += " ";
	}
}


Then you can add reverse += "."; to get the dot at the end of the sentence.
And this is how I got the first character to be uppercase:
reverse = reverse.replaceFirst(Character.toString(reverse.charAt(0)), Character.toString(reverse.charAt(0)).toUpperCase());

Was This Post Helpful? 0
  • +
  • -

#9 constden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:36 AM

I got the code to work for me. Thank you PeanutButter :)
Was This Post Helpful? 0
  • +
  • -

#10 PeanutButter  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 28-March 09

Re: Need help with for loop

Posted 25 April 2009 - 11:39 AM

You're welcome. Feel free to ask again if you need anything else.
Was This Post Helpful? 0
  • +
  • -

#11 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:43 AM

Well, since you're not supposed to write people's code for them I was trying to give constden the problem and an idea and let him (her?) do what he (she?) wanted…but yeah, PeanutButter's works way better than mine. :)
Was This Post Helpful? 0
  • +
  • -

#12 PeanutButter  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 28-March 09

Re: Need help with for loop

Posted 25 April 2009 - 11:47 AM

I posted my working code because we saw the he was trying. If there wouldn't have been any effort on his side to do his homework I wouldn't have posted the code :P
Was This Post Helpful? 0
  • +
  • -

#13 constden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 09

Re: Need help with for loop

Posted 25 April 2009 - 11:47 AM

I pretty much just ended up putting that necessary "+" before the equal sign, then trimmed my string so any spaces at front or end would be discarded and used the code I had already written to capitalize the first letter and add a period.
Thank you guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1