Results 1 to 4 of 4

Thread: loop help

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default 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("");
    
            
            }
         }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bgreen7887 View Post
    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..
    So what problems are you having with this code? What's not working?

  3. #3
    SQUALL is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    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 :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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

  1. While loop for jpg
    By TheBigB in forum New To Java
    Replies: 1
    Last Post: 07-29-2010, 08:25 PM
  2. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  3. For Loop
    By YiBoog in forum New To Java
    Replies: 6
    Last Post: 11-11-2009, 07:53 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. While loop
    By verbazon in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 01:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •