We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,414 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

java

help i cant figure it out.

Write a program that defines and instantiates a new String object called title with COMPUTER PROGRAMMING in it. Then create a new String labeled title2 -- write code to store the contents of title into title1 in reverse order. Using a for loop and charAt.

3
Contributors
5
Replies
20 Hours
Discussion Span
7 Months Ago
Last Updated
6
Views
applepie-0167
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Huh? This is JavaScript forum. You should go to this forum instead.

To answer your question, it said that you to create 2 String variables. Assign the value "COMPUTER PROGRAMMING" to the first one, and then iterate through a loop using the first variable length in reverse order. Inside the loop, append each character to the second string using charAt() method. Nothing saying about display the result, but you should be able to by using System.out.print() or System.out.println() method to do so.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Write a program that defines and instantiates a new String object called title with COMPUTER PROGRAMMING in it. Then create a new String labeled title2 -- write code to store the contents of title into title1 in reverse order. Using a for loop and charAt.

You don't need a loop nor a charAt method for this task; The "for in" is sllower + collects colaterals, -you don't need the "charAt" either (also sllow and completely unnecesary). String Objects are also sllow, string literals are faster. So instead of learning the worst approach possible even if being suggested by your school, I propose a professional and efficient coding which will in some degre, satisfy both sides:

var title = "COMPUTER PROGRAMMING",
    title1 = new String( title ), 
    title2 = new String(title.split("").reverse().join(""));

title2 >> "GNIMMARGORP RETUPMOC"

Troy III
Master Poster
747 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

Sorry Troy, the requirement said that the OP needs to use charAt() method to solve the problem. Besides, there is no reverse() or join() method for String class in Java (not JavaScript).

/*
i.e in Java
String title1 = "COMPUTER PROGRAMMING";
String title2 = "";  // if not use StringBuilder class
for (...) {  // do the loop in reverse
  // rebuild the string here one char by one char
}
*/
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

Thanks for the feedback lol but applepie-0167 is obviously thinking javascript while saying java, and this, of course is a javascrip section, therefore a javascript solutions will be served.
p.s. There's no 'String.reverse' method in javascript either, but there we are. It's the power of Live Script at work! :)

Troy III
Master Poster
747 posts since Jun 2008
Reputation Points: 140
Solved Threads: 95
Skill Endorsements: 5

No no Troy, many people who are new to programming often times are confused that Java is JavaScript and vice versa. This requirement is more on programming concept which is DIY. To learn some logics, you may need to learn how a built-in function/method works by implementing it yourself.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0713 seconds using 2.71MB