Results 1 to 18 of 18
  1. #1
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default How do I fix this program (2 errors)?

    This program needs to accept an input (Odd number, between 3-5) and then it will use that number to output a Diamond. The user input will be the center of the diamond. For example, if the user inputs 9 the diamond will look like this

    (Note: the formatting rules of this forum delete the spaces so I can't format this as a proper diamond but you get the idea)

    *
    ***
    *****
    *******
    *********
    *******
    *****
    ***
    *
    I get 2 errors:

    The local variable diamondString may not have been initialized
    Unhandled exception type IOException

    Java Code:
    public class  Diamond {
    	
    
    	
    	public static void main(String[] args)
    	{
    		CollectInput();
    	
    	
    	}
    	public static void CollectInput()
    	{
    	
    		int num;
    	
    	System.out.println("Please input an odd number between 3 and 15.");
    	num = System.in.read();
    	
    	}
    	
    	public void CreateRows(int num)
    	{
    		int row;
    		int spaces;
    		int stars;
    		String diamondString;
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(2 *row)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(row/2)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		}
    }

    I don't know if my logic is right for the top half of the diamond but i'm not sure about the bottom. I'm hoping being able to see the output will allow me to guess/check the logic until it's correct.

    Any help would be appreciated
    Last edited by Fubarable; 07-17-2011 at 05:25 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try initializing diamondString to an empty string(alternatively, a better approach is to use a string builder and append rather than the slower string concatenation). As for the second exception, I'm guessing it's caused by "System.in.read", You should really use a scanner instead.

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

  4. #4
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Try initializing diamondString to an empty string(alternatively, a better approach is to use a string builder and append rather than the slower string concatenation). As for the second exception, I'm guessing it's caused by "System.in.read", You should really use a scanner instead.
    I'm not familiar with that. My understanding of programing is very basic.

    Quote Originally Posted by Fubarable View Post
    Where do you call the createRows method?
    Would that just involve putting

    CreateRows();

    in main()?

    ------------

    I've updated my code to include what was mentioned above. I only have 1 error:

    The method CreateRows(int) in the type Diamond is not applicable for the arguments ()


    Java Code:
    import java.util.Scanner;
    
    public class  Diamond {
    	
    
    	
    	public static void main(String[] args)
    	{
    		CollectInput();
    		CreateRows();
    	
    	
    	}
    	public static void CollectInput()
    	{
    	
    	
    	System.out.println("Please input an odd number between 3 and 15.");
    	
    	Scanner num = new Scanner(System.in);
    	
    	}
    	
    	public void CreateRows(int num)
    	{
    		int row;
    		int spaces;
    		int stars;
    		String diamondString = "";
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(2 *row)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(row/2)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		}
    }
    Thanks for the help so far.
    Last edited by Logik22; 07-17-2011 at 06:44 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    I only have 1 error:
    Please copy and paste the full text of the error message. There is information in the message that is important.

  6. #6
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste the full text of the error message. There is information in the message that is important.
    I did copy it below it.

    The method CreateRows(int) in the type Diamond is not applicable for the arguments ()

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

    Default

    The error is telling you what is wrong. Your createRows method should have an int passed into its parameter and you're calling it without this. Also all method names should begin with lower case letters if you want others (us and your instructors for instance) to better understand your code.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    What compiler are you using? I don't recognize that as the text of an error message.
    My compiler gives this:
    Java Code:
    TestCode5.java:95: aMethod(int) in TestCode5 cannot be applied to ()
          aMethod();
          ^
    1 error
    Have you looked at the method referenced in the error message?
    How are you calling it?
    How is it defined?
    They must match.

  9. #9
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The error is telling you what is wrong. Your createRows method should have an int passed into its parameter and you're calling it without this. Also all method names should begin with lower case letters if you want others (us and your instructors for instance) to better understand your code.
    I have updated main() with createRows(num) but that brings up a new error:

    num cannot be resolved to a variable


    I initialize num in collectInput()



    Java Code:
    import java.util.Scanner;
    
    public class  Diamond {
    	
    	
    
    	
    	public static void main(String[] args)
    	{
    		collectInput();
    		createRows(num);
    	
    	
    	}
    	public static void collectInput()
    	{
    	
    	
    	System.out.println("Please input an odd number between 3 and 15.");
    	
    	Scanner sc = new Scanner(System.in);
    	int num = sc.nextInt();
    
    	
    	}
    	
    	public void createRows(int num)
    	{
    		int row;
    		int spaces;
    		int stars;
    		String diamondString = "";
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(2 *row)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(row/2)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		}
    }

  10. #10
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What compiler are you using? I don't recognize that as the text of an error message.
    My compiler gives this:
    Java Code:
    TestCode5.java:95: aMethod(int) in TestCode5 cannot be applied to ()
          aMethod();
          ^
    1 error
    Have you looked at the method referenced in the error message?
    How are you calling it?
    How is it defined?
    They must match.
    I am using Eclipse.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    I initialize num in collectInput()
    However that variable is defined local to collectInput. It needs to be defined outside the method for other methods to see it.

  12. #12
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    However that variable is defined local to collectInput. It needs to be defined outside the method for other methods to see it.
    Ahh okay. That makes sense.

    So generally where is the best place to define variables that will be used by multiple methods?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    At the class level. Some people like them all at the front. Some like them all at the back.
    Occasionally just before a method that wants the values saved over calls.

    I prefer the front. Looking at the variables and their names can tell you a lot about what the code is doing.
    If your names are generated names like jTextField1 then it doesn't matter. The name tells you nothing.

  14. #14
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok, I have updated my code.

    I get no bugs except but I try to run the program it says:

    Fatal exception occurred. Program will exit.

    In the console it says

    java.lang.NoSuchMethodError: main

    Java Code:
    public class  Diamond {
    	
    	
    int num;
    	
    	public void main(String[] args)
    	{
    		collectInput(num);
    		createRows(num);
    	
    	
    	}
    	public void collectInput(int num)
    	{
    	
    	
    	System.out.println("Please input an odd number between 3 and 15.");
    	
    	Scanner sc = new Scanner(System.in);
    	num = sc.nextInt();
    
    	
    	}
    	
    	public void createRows(int num)
    	{
    		int row;
    		int spaces;
    		int stars;
    		String diamondString = "";
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(2 *row)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(row/2)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		}
    }

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    How are you running the program?
    How do you get those two error messages?

    The second error means that no code could be found to be executed.
    The main method must be static.

    The first one sounds like it can from an executing program.
    Last edited by Norm; 07-17-2011 at 07:52 PM.

  16. #16
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How are you running the program?
    How do you get those two error messages?

    The second error means that no code could be found to be executed.
    The main method must be static.

    The first one sounds like it can from an executing program.
    In Eclipse I go to "Run>Run".

    I changed main() to static and now I get 2 errors with the 2 methods being called within main().

    collectInput(num);
    createRows(num);

    Cannot make a static reference to the non-static field num


    So is this because I initialized num in the class? If so, does that mean I must take it out of there and initialize it in each of the different static methods?

    Thanks for the help!
    Java Code:
    public class  Diamond {
    	
    	
    int num;
    	
    	public static void main(String[] args)
    	{
    		collectInput(num);
    		createRows(num);
    	
    	
    	}
    	public void collectInput(int num)
    	{
    	
    	
    	System.out.println("Please input an odd number between 3 and 15.");
    	
    	Scanner sc = new Scanner(System.in);
    	num = sc.nextInt();
    
    	
    	}
    	
    	public void createRows(int num)
    	{
    		int row;
    		int spaces;
    		int stars;
    		String diamondString = "";
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(2 *row)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		
    		for (row = 1; row  <=num/2+1;row ++)
    		{
    			for (spaces = num; spaces >row; spaces--)
    			{
    				 diamondString += " ";
    				
    			}
    			for(stars=1; stars <=(row/2)-1;stars++)
    			{
    				diamondString +="*";
    			}
    				diamondString +="\n";
    		}
    		System.out.println(diamondString);
    		}
    }

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Why do you need the num variable as a class variable? Can it be local to main()?

    The way you are writing the code, all methods will need to be static to be called from the main() method.
    The non-static methods you are trying to call from main exist when the Diamond object is created.
    Create an instance of the Diamond object and use its reference to call its methods.

    Also you have local variables that "shadow" the class variable num.
    Java Code:
    public void collectInput(int num)
    This num hides the variable with the same name at the class level.

    A better approach would be for the collectInput method to return the value it reads vs setting a global variable.

  18. #18
    Logik22 is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Why do you need the num variable as a class variable? Can it be local to main()?

    The way you are writing the code, all methods will need to be static to be called from the main() method.
    The non-static methods you are trying to call from main exist when the Diamond object is created.
    Create an instance of the Diamond object and use its reference to call its methods.

    Also you have local variables that "shadow" the class variable num.
    Java Code:
    public void collectInput(int num)
    This num hides the variable with the same name at the class level.

    A better approach would be for the collectInput method to return the value it reads vs setting a global variable.
    Alright, I will work with this.

    Thanks again for your help!

Similar Threads

  1. Help with some errors in my program
    By kbud123 in forum Java Applets
    Replies: 5
    Last Post: 05-11-2011, 05:08 AM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Errors with simple program... PLEASE HELP ME!!!
    By maxpower1000sa in forum New To Java
    Replies: 6
    Last Post: 05-03-2009, 11:55 PM
  4. Help with Errors in Inventory Program
    By ljk8950 in forum AWT / Swing
    Replies: 3
    Last Post: 08-08-2008, 11:49 PM
  5. 3 errors and then terminate program
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 05-20-2008, 01:57 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
  •