Question:
Java Programming: Loops?
epicfailatjava
2008-10-20 13:26:23 UTC
Ok, I have a problem with loops in Java specifically one of the questions I have stumbled upon and need to figure out how to solve it. The question asks the programmer to create a program that ask the user to input a number. This number is then taken apart and displayed on separate lines. So the number 32 would end up being:
3
2
I know how to program the input and output statements in I just have problems doing the loop and it has to be a loop.
Three answers:
James C
2008-10-20 13:34:20 UTC
The input from the user would be in string format.

Get the length of the string with String.length();



Then you will want to loop through each character of the string.



int i=0;

while (i < String.length()){

System.out.println(String.charAt(i)); //reference the char at that pos.

i++; //i add one

}



I haven't tested this but I am pretty sure it will work.
rpl oye
2008-10-20 20:37:08 UTC
dont use "writeln", use "write" command

i rarely use java as my programming language, but in many programming language, there are a command that put a line space after writing some string (writeln) and a command that not (write).



or maybe you use "\n" after the output string,



put up some of your code here so we can look and help you help me help you..lol
anonymous
2008-10-20 20:33:15 UTC
Your input is going to initially be a string. Then just use the chatAt(int) method to display the various characters



String input=(some input)

for(int i=0;i
System.out.println(input.charAt(i));

}



And done.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...