Results 1 to 6 of 6
  1. #1
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default What is wrong with this code?

    This is the error message that JGrasp gives me

    Java Code:
     ----jGRASP exec: javac -g Crammer.java
    
    Crammer.java:15: error: variable xcoor might not have been initialized
    			xcoor[i] = keyboard.nextInt();
    			^
    Crammer.java:17: error: variable ycoor might not have been initialized
    			ycoor[i] = keyboard.nextInt();
    			^
    Crammer.java:22: error: variable xcoor might not have been initialized
    			System.out.println("(" + xcoor[i] + "," + ycoor[i] + ")");
    			                         ^
    Crammer.java:22: error: variable ycoor might not have been initialized
    			System.out.println("(" + xcoor[i] + "," + ycoor[i] + ")");
    			                                          ^
    4 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    The following is my code

    Java Code:
    import java.util.*;
    
    public class Crammer
    {
    	public static void main(String[] args)
    	{
    		int xcoor[];
    		int ycoor[];
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("How many sides are in your polygon?");
    		int n = keyboard.nextInt();
    		for(int i = 0; i < n; i++)
    		{
    			System.out.println("What is the x coordinate of side " + i + "?");
    			xcoor[i] = keyboard.nextInt();
    			System.out.println("What is the y coordinate of side " + i + "?");
    			ycoor[i] = keyboard.nextInt();
    		}
    		// temporary debug
    		for(int i = 0; i < n; i++)
    		{
    			System.out.println("(" + xcoor[i] + "," + ycoor[i] + ")");
    		}
    	}
    }
    I've been stuck trying to figure this one out for a while, according to google everything should work fine but there's probably something I'm not seeing.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: What is wrong with this code?

    variable xcoor might not have been initialized
    The compiler can not find where that array has been given any value. After defining the name of the array you must assign it the number of elements it is to contain with a new statement or with a list of elements enclosed in {}s.
    Arrays are a little like objects.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: What is wrong with this code?

    you should initialize the array at first. however, since your length of your array is related to "n", so you should do
    the initialization after get "n". so, after "int n = keyboard.nextInt", do the initialization like:
    int[] xcoor = new int[n];
    int[] ycoor = new int[n];
    after doing this, no problem for your code.

  4. #4
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: What is wrong with this code?

    ("What is the x coor... .. (i+1) + "?"); // Asking for a zero side x and y was odd.
    Last edited by rru96; 09-22-2012 at 04:28 AM.

  5. #5
    ayershov777 is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: What is wrong with this code?

    Quote Originally Posted by thinkinjava24 View Post
    you should initialize the array at first. however, since your length of your array is related to "n", so you should do
    the initialization after get "n". so, after "int n = keyboard.nextInt", do the initialization like:
    int[] xcoor = new int[n];
    int[] ycoor = new int[n];
    after doing this, no problem for your code.
    Thanks a lot man, that fixed my problem.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: What is wrong with this code?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. What is wrong with this code?
    By gkoef in forum New To Java
    Replies: 5
    Last Post: 05-29-2011, 05:31 PM
  2. What is wrong with my code?
    By loosemember in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 01:20 AM
  3. What's wrong with my code?
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 06:00 PM
  4. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 11:28 AM
  5. what's the wrong in this Code
    By the swan in forum AWT / Swing
    Replies: 1
    Last Post: 04-04-2009, 04:27 AM

Tags for this Thread

Posting Permissions

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