Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: what is wrong in this code

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong in this code

    import java.util.*;
    public class charPerLine {
     
     
     
    	public static void main(String[] args)throws StringIndexOutOfBoundsException {
    	Scanner sc=new Scanner(System.in);
         System.out.print("Type any name:");
         String s=sc.next();
                 int j= s.length();
         for(int i=0;i<=j;i++){
         char ch=s.charAt(i);
         System.out.println(ch);
         }
     
    	}
     
    }
    Last edited by copeg; March 6th, 2011 at 10:20 AM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: what is wrong in this code

    For future reference, please flank your code with the [highlight=java][/highlight] tags. I've edited your post for you.

    what is wrong in this code
    You need to tell us...does it compile? Are there exception? Does it behave? What exactly is the problem and/or question?
    Last edited by copeg; March 6th, 2011 at 10:46 AM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: what is wrong in this code

    I think it should be going from 0 to less than j. Not less than or equal to j.

    import java.util.*;
    public class charPerLine {
     
     
     
    	public static void main(String[] args)throws StringIndexOutOfBoundsException {
    	Scanner sc=new Scanner(System.in);
         System.out.print("Type any name:");
         String s=sc.next();
                 int j= s.length();
         for(int i=0;i<j;i++){
         char ch=s.charAt(i);
         System.out.println(ch);
         }
     
    	}
     
    }

Similar Threads

  1. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 04:30 PM
  2. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 08:59 AM
  3. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 04:21 AM
  4. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 02:38 AM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM

Tags for this Thread