Results 1 to 4 of 4
Thread: loop help
- 10-17-2010, 11:53 PM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
loop help
Hey guys trying to get this program to Recieve input from user(a string), then print each character of the String to its own line..
Java Code:import java.util.Scanner; public class ap { public static void main(String[]args) { String b; String c; Scanner j1 = new Scanner(System.in); System.out.println("Enter your String please!!"); b = j1.nextLine(); int index1 = b.charAt(0); int index2 = b.charAt(b.length()-1); while ( index1 >=0 ) { System.out.println(""); } } }
-
- 10-18-2010, 10:31 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Hi ...
OK ... I hope this code can help you :)
import java.util.Scanner;
public class ap {
public static void main(String[] args) {
String b;
String c;
Scanner j1 = new Scanner(System.in);
System.out.println("Enter your String please!!");
b = j1.nextLine();
// here we declare char array ...
char[] ch;
// put each char of string in cell of array
ch = b.toCharArray();
// finally print each char
for (int i = 0; i < ch.length; i++) {
System.out.println(ch[i]);
}
}
}
I hope that answered your question :)
- 10-18-2010, 10:54 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Squall: please use the code tags so that your code is readable.
Posting code solutions is frowned upon. In this case it doesn't answer the OP's question ... because the OP did not ask a question!
Instead you might have pointed out (rather than repeated) the oddities in the OP's code: the strange naming conventions and indenting and the declared (or assigned) variables that were never used.
But by far the biggest help for the OP would come by leading him or her to actually ask a question. It is by describing things (our intentions, our attempts, their results) that we gradually begin to obtain a clue. Expressing things helps to obtain the best help from others but, almost magically, expressing things helps to clarify our own thoughts.
I don't know about you, but I find the process of "obtaining a clue" about some technology I'm not used to, rather hard work. The problem with code handouts is that they provide a deceptively compelling way out: they address the momentary problem of some code or other that doesn't "work", but they do nothing to foster the mental attitude that is capable of framing and solving a problem.
Similar Threads
-
While loop for jpg
By TheBigB in forum New To JavaReplies: 1Last Post: 07-29-2010, 08:25 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
For Loop
By YiBoog in forum New To JavaReplies: 6Last Post: 11-11-2009, 07:53 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
While loop
By verbazon in forum New To JavaReplies: 5Last Post: 02-25-2009, 01:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks