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: Code doesn't execute properly

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

    Default Code doesn't execute properly

    What the code is supposed to do is if the user does not enter an odd number it asks to do it again. Then if an odd number is entered it creates a diamond pattern.
    Right now If I enter an even number, it asks to enter an odd number, but it doesn't allow me to do it. If I enter an odd number first, then the code executes properly.

    I'm aware that the code is kind of sloppy as I am new to coding.

    import java.io.*;
    import java.util.Scanner;
     
    public class Diamond {
    	public static void main(String [] args) throws IOException {
     
    	BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    	String input; 
    	int num;
    	System.out.println("input number: ");
    	input = stdin.readLine ();
    	num = Integer.parseInt(input);
     
    	if (num % 2 ==1){
    	int d = num;
    	int e = 0;
    	for (int a = 0; a <= num; a++) {
    	for (int c = d; c>= 1; c-- )
    	System.out.print(" ");
    	d-=1;
    	for (int b = 1; b <= a; b++)
    	System.out.print ("* ");
    	System.out.println();
    	}
    	num-=1;
    	for (int a = 0; a<=num; a++) {
    	for (int b = num; b > a; b--)
    	System.out.print (" *");
    	System.out.println(); 
    	for (int c = 0; c <= e; c++)
    	System.out.print(" ");
    	e+=1;
     
    	}}else
    	 {System.out.println("Please enter an odd number!");
    	 }
    	}
    	}


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code doesn't execute properly

    The reason for this is that you never state that the program should. Look in your else method. It says "System.out.println(....); " and then nothing else
    One way to do it is to wrap the method in a while(true) loop, and in the first if statement, at the end put break; . like this:
    import java.io.*;
    import java.util.Scanner;
     
    public class Diamond {
    	public static void main(String [] args) throws IOException {
    	System.out.println("input number: ");
     while(true)
     {
    	BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    	String input; 
    	int num;
     
    	input = stdin.readLine ();
    	num = Integer.parseInt(input);
     
    	if (num % 2 ==1){
    	int d = num;
    	int e = 0;
    	for (int a = 0; a <= num; a++) {
    	for (int c = d; c>= 1; c-- )
    	System.out.print(" ");
    	d-=1;
    	for (int b = 1; b <= a; b++)
    	System.out.print ("* ");
    	System.out.println();
    	}
    	num-=1;
    	for (int a = 0; a<=num; a++) {
    	for (int b = num; b > a; b--)
    	System.out.print (" *");
    	System.out.println(); 
    	for (int c = 0; c <= e; c++)
    	System.out.print(" ");
    	e+=1;
     
    	}
    	 break;
    	}else
    	 {System.out.println("Please enter an odd number!");
    	 }
    	}
    	}
    	}

    Note that it prints "input number" before the while loop starts so that it doesn't repeatedly print it.

  3. The Following User Says Thank You to minime12358 For This Useful Post:

    fride360 (March 12th, 2011)

Similar Threads

  1. unable to execute prepared statement
    By nrao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2010, 08:26 PM
  2. What is SNMP in Java?Hoe to execute code related to this?
    By jj_crazy_1 in forum Java Theory & Questions
    Replies: 0
    Last Post: April 5th, 2010, 10:47 PM
  3. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM
  4. How to execute a Java program independent of Netbeans.
    By ShaunB in forum Java Theory & Questions
    Replies: 4
    Last Post: January 19th, 2010, 06:23 AM
  5. how to execute a simple display without a main method
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 13th, 2010, 07:28 AM