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 2 of 2

Thread: For loop not giving the correct output

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For loop not giving the correct output

    I am attempting to get all the letters A-Z to print on the screen with 10 letters per line. I created a method <private static char printChars(char ch1, char ch2, int numberPerLine> to do this and all the letters print out, only they are on the same line, and not 10 per line. Here is the code...I'm pretty sure that the problem is in the if/else statement.

    public class Lab5_12A {
    	public static void main(String[] args) {
    		// Define variables and assign value
    		char character1 = 'A';
    		char character2 = 'Z';
    	    int  number = 10; 
     
    		for (character1 = 'A'; character1 <= character2; character1++) {
    			System.out.printf(printChars(character1, character2, number) + " ");
    		    }
    	}
    		private static char printChars(char ch1, char ch2, int numberPerLine) {
    			char result;
     
    			if (numberPerLine <= 10)
    				result = ch1;		
    			else
    				result = ch2;
     
    			return result;
    			}
    	 }

    I am still really new to Java and have only been in class for about 4 weeks, so any help is appreciated!


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: For loop not giving the correct output

    Try this:

    public class Lab5_12A {
    	public static void main(String[] args) {
    		// Define variables and assign value
    		char character1 = 'A';
    		char character2 = 'Z';
    	    int  number = 0; 
     
    		for (character1 = 'A'; character1 <= character2; character1++) {
     
    			System.out.printf(printChars(character1, character2, ++number) + " ");
    		    }
    	}
    		private static String printChars(char ch1, char ch2, int numberPerLine) {
    			String result;
     
    			if (numberPerLine % 10==0) {
     
                            result =ch1+"\n";
     
                        }		
    			else {
                                if(numberPerLine==1){
                                   System.out.printf(" ");
     
                                   result =""+ch1 ;
                                }
                            result = ch1+"";
                        }
     
    			return result;
    			}
    	 }

    I hope this will be help to you
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 15th, 2013, 08:35 PM
  2. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 10:31 PM
  3. My loops are working, but not giving right output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 14th, 2013, 07:55 PM
  4. Not getting the correct output
    By KNAYERS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 23rd, 2012, 01:59 PM
  5. [SOLVED] No errors, but output not correct? Only one loop performed correctly! Help please :)
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2012, 11:18 AM