Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Creating a 4x4 gird

    I am having trouble developing a 4x4 grid that has the letters A-Z and a-z and the corresponding ASCII numbers for each letter. I have created the code to retrieve these letters and their corresponding ASCII numbers, I only am having trouble with creating the grid and displaying these numbers and letters in their separate 4x4 grids: one grid for (a-z), one grid for (A-Z), one grid for (ASCII# a-z), one grid for (ASCII# A-Z).

    Java Code:
    	// Constants Declaration Section
    		//*******************************
    
    		
    		
    		public static void main(String[] args) 
    		{
    
    			// Variables Declaration Section
    			//******************************
    			
    			
    
    		    
    
    		    
    		    // Variables Initialization Section
    		    //*********************************
    			
    			
    			
    			//Keyboard
    			//********
    			
    
    		    
    		    // Code Section
    		    //*************
    			for(int i = 65; (i >=65 && i <= 90); i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{
    				System.out.print((char) i + " - " + i + "    "); //Displays the letter assigned the current number
    					
    			}
    			
    			System.out.print("\n\n");
    			
    			for(int i = 97; i >= 97 && i <= 122; i++)  //Creates a loop that starts at the number 97 and stops at 122 (A- Z)
    			{ 
    				System.out.print((char) i + " - " + i + "    ");   //Displays the letter assigned the current number
    		
    			}
    			
    			
    	          
    	       
    		    
    
    
    		    
    		    // Output Section
    		    //***************
    			
    			
    
    			
    			// Cleaning Section
    			//*******************
    			
    		
    
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Creating a 4x4 gird

    A couple of observations.

    You haven't said exactly what the grid is supposed to look like so it is hard to help you. Since there is not much code here I can't but wonder if you haven't come to the forum too quickly. You need to think about what you are trying to do. Draw it on paper if you need to.

    I can tell you for certain that in the loops, i will never be < 65 in the first loop nor 97 in the second. So why are you testing for it?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating a 4x4 gird

    Note: char are numeric values and can be used directly in the code instead of their int values.
    That makes the code more readable. For example 'A' can be used in place of 65 and 'a' for 97
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Yeah there isn't much code, haha! I was told to get the letters and ASCII numbers corresponding to those letters. I have to put these numbers and letters in a grid format, specifically a 4x4 grid. My professor said he did it with a *if/else* statement. I tried to use arrays, but I don't know how to implement what I wrote into that.

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by Norm View Post
    Note: char are numeric values and can be used directly in the code instead of their int values.
    That makes the code more readable. For example 'A' can be used in place of 65 and 'a' for 97
    I used numbers so that I could get the corresponding letter for that number, guess I could of done 'A' then (int) 'A'.

  6. #6
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    OK so this is what i've got

    Java Code:
    for(int i = 65; (i >=65 && i <= 90); i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
    				int col_count = 1;
    				for(int j = 97; j >= 97 && j <= 122; i++; col_count++) //Displays the letter assigned the current number
    				{ 
    					matrix[acsiiTable][col_count] = j;
    				}
    			}
    Im getting an error here
    Java Code:
     i++[B];[/B] col_count++)
    it says to replace with , , but when i do it gives me an error again.

    HELP :(
    Last edited by sithclone3; 02-07-2015 at 05:19 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating a 4x4 gird

    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Look at the tutorial on how to code a for statement:
    http://docs.oracle.com/javase/tutori...bolts/for.html
    Last edited by Norm; 02-06-2015 at 11:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Creating a 4x4 gird

    It is talking about this:

    Java Code:
    for (int i = 65'; i <= 90; i++, col_count++) {
    // other stuff
    }
    But why are you doing that? You were told earlier you could do it this way.

    Java Code:
    for (int i = 'A'; i <= 'Z'; i++) {
      // just use i - 'A' instead of a separate counter.  If you need to use it often, assign it
      // to a variable inside the loop:
      // e.g. int t = i - 'A';
    }
    And you don't need to check for i <= 65 as i is only increasing. But first, you need to think about your logic and what you are trying to do.

    First, you said a 4 x 4 grid. Don't you mean a 2 x 2 grid? Your description implies that. A 2 x 2 grid has 4 squares. Simply decide how many lines in each grid. Think about when you need to print new lines and when stuff needs to be on the same line. You may want to use printf to control format spacing. But most important is to work this out on paper (perhaps grid paper) and see how it looks. Then try and duplicate it in Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by Norm View Post
    Please copy full text of the compiler's error message and paste it here.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Look at the tutorial on how to code a for statement:
    The for Statement (The Java‚„Ę Tutorials > Learning the Java Language > Language Basics)
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on token ";", , expected
    Last edited by sithclone3; 02-07-2015 at 05:19 AM.

  10. #10
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by jim829 View Post
    It is talking about this:

    Java Code:
    for (int i = 65'; i <= 90; i++, col_count++) {
    // other stuff
    }
    But why are you doing that? You were told earlier you could do it this way.

    Java Code:
    for (int i = 'A'; i <= 'Z'; i++) {
      // just use i - 'A' instead of a separate counter.  If you need to use it often, assign it
      // to a variable inside the loop:
      // e.g. int t = i - 'A';
    }
    And you don't need to check for i <= 65 as i is only increasing. But first, you need to think about your logic and what you are trying to do.

    First, you said a 4 x 4 grid. Don't you mean a 2 x 2 grid? Your description implies that. A 2 x 2 grid has 4 squares. Simply decide how many lines in each grid. Think about when you need to print new lines and when stuff needs to be on the same line. You may want to use printf to control format spacing. But most important is to work this out on paper (perhaps grid paper) and see how it looks. Then try and duplicate it in Java.

    Regards,
    Jim
    Well im trying to retrieve all the letters of the english alphabet, uppercase and lowercase, and their ASCII numbers. then create a matrix with all the upper case letters, a seperate matrix with all the lower case letters, another matrix with the ASCII numbers for the uppercaase letters and a last matrix with the ASCII numbers for the lowercase letters...

  11. #11
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Java Code:
    for(int i = 65; (i >=65 && i <= 90); i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
    				int col_count = 1;
    				for(int j = 97; j >= 97 && j <= 122; i++; col_count++) //Displays the letter assigned the current number
    				{ ......................................^
    					matrix[acsiiTable][col_count] = j;
    				}
    			}



    sorry its being weird by making the "^" go to the next line, so i added the dots

    Error message:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on token ";", , expected
    Last edited by sithclone3; 02-07-2015 at 05:22 AM.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Creating a 4x4 gird

    The compiler is telling you exactly what is wrong: the for statement looks like this: for(initialization; condition; adjustment) ... and none of the three parts can contain a semi colon; now look again at your 'adjustment' part: i++; col_count++. See what's wrong with it?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by JosAH View Post
    The compiler is telling you exactly what is wrong: the for statement looks like this: for(initialization; condition; adjustment) ... and none of the three parts can contain a semi colon; now look again at your 'adjustment' part: i++; col_count++. See what's wrong with it?

    kind regards,

    Jos
    I see what you mean, and I have changed it, but then the matrix doesn't work and becomes invalid...

    Regards,
    Sith

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by sithclone3 View Post
    I see what you mean, and I have changed it, but then the matrix doesn't work and becomes invalid...
    Did you change the semicolon in the adjustment part to a comma?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating a 4x4 gird

    the matrix doesn't work and becomes invalid...
    Can you post the output that shows what you are talking about
    and the code that generates that output.

    Also add some comments describing what is wrong with the output and show what it should be.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Java Code:
    for(int i = 65; i <= 90; i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
    				int col_count = 1;
    				for(int j = 97; j <= 122; i++; col_count++) //Creates a loop that starts at the number 97 and stops at 122 (A-Z)
    				{                            ^
    					matrix[acsiiTable][col_count] = j;  //Creates matrix
    				}
    			}
    Output Error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on token ";", , expected


    After Change:

    Java Code:
    for(int i = 65; i <= 90; i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
                       ^
    				int col_count = 1;
    				for(int j = 97; j <= 122; i++, col_count++) //Creates a loop that starts at the number 97 and stops at 122 (A-Z)
    				{ 
    					matrix[acsiiTable][col_count] = j;  //Creates matrix
                         ^
    				}
    			}
    Problem:
    The local variable matrix may not have been initialized

    Only Solution to fix the second issue after changing the ';' to ',' is to make 'int[][] matrix = null' and that makes the program not work.

    This is my full code
    Java Code:
    public class ASCII {
    		// TODO Auto-generated method stub
    
    		// Constants Declaration Section
    		//*******************************
    
    		
    		
    		public static void main(String[] args) 
    		{
    
    			// Variables Declaration Section
    			//******************************
    			int acsiiTable; 
    			int[][] matrix;
    		    
    
    		    
    		    // Variables Initialization Section
    		    //*********************************
    			acsiiTable = 0;
    			
    			
    			//Keyboard
    			//********
    			
    
    		    
    		    // Code Section
    		    //*************
    			
    			for(int i = 65; i <= 90; i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
    				int col_count = 1;
    				for(int j = 97; j <= 122; i++, col_count++) //Creates a loop that starts at the number 97 and stops at 122 (A-Z)
    				{ 
    					matrix[acsiiTable][col_count] = j;  //Creates matrix
    				}
    			}
    			
    			
    			
    			
    			/*System.out.print("\n\n");
    			
    			for(int i = 97; i >= 97 && i <= 122; i++)  //Creates a loop that starts at the number 97 and stops at 122 (A- Z)
    			{ 
    				System.out.print((char) i + " - " + i + "    ");   //Displays the letter assigned the current number
    			}*/
    			
    			
    			
    	          
    	       
    		    
    
    
    		    
    		    // Output Section
    		    //***************
    			
    			
    
    			
    			// Cleaning Section
    			//*******************
    			
    		
    
    	}
    
    }
    Last edited by sithclone3; 02-07-2015 at 06:04 PM.

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

    Default Re: Creating a 4x4 gird

    local variable matrix may not have been initialized
    You need to use a new statement to assign the matrix value a value.
    See the tutorial: http://docs.oracle.com/javase/tutori...ts/arrays.html
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Ok, so I assigned 'matrix' a value. But now I'm getting this error.

    Here is my new code:

    Java Code:
    /*
     * Student Name: Sean Pratt
     * 
     * Description: Creating letters from numbers using the ASCII scale
     * 
     * Input Variables:
     * 
     * Desired Output: 
     */
    public class ASCII {
    		// TODO Auto-generated method stub
    
    		// Constants Declaration Section
    		//*******************************
    
    		
    		
    		public static void main(String[] args) 
    		{
    
    			// Variables Declaration Section
    			//******************************
    			int acsiiTable; 
    			int[][] matrix;
    		    
    
    		    
    		    // Variables Initialization Section
    		    //*********************************
    			acsiiTable = 0;
    			matrix = new int[5][6];
    			
    			
    			
    			//Keyboard
    			//********
    			
    
    		    
    		    // Code Section
    		    //*************
    			
    			for(int i = 65; i <= 90; i++)  //Creates a loop that starts at the number 65 and stops at 90 (a-z)
    			{				
    				matrix[acsiiTable][0] = i; 
    				int col_count = 1;
    				for(int j = 97; j <= 122; i++, col_count++) //Creates a loop that starts at the number 97 and stops at 122 (A-Z)
    				{ 
    					matrix[acsiiTable][col_count] = j;  //Creates matrix
    				}
    			}
    			
    			
    			
    			
    			/*System.out.print("\n\n");
    			
    			for(int i = 97; i >= 97 && i <= 122; i++)  //Creates a loop that starts at the number 97 and stops at 122 (A- Z)
    			{ 
    				System.out.print((char) i + " - " + i + "    ");   //Displays the letter assigned the current number
    			}*/
    			
    			
    			
    	          
    	       
    		    
    
    
    		    
    		    // Output Section
    		    //***************
    			
    			
    
    			
    			// Cleaning Section
    			//*******************
    			
    		
    
    	}
    
    }
    New error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at ASCII.main(ASCII.java:49)

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating a 4x4 gird

    java.lang.ArrayIndexOutOfBoundsException: 6
    at ASCII.main(ASCII.java:49)
    At line 49 the program used an index past the end of the array. The valid indexes for an array range from 0 to the array's length-1. For example a 6 element array's indexes are 0 to 5.
    Look at line 49 and see what index variable had the invalid value and then backtrack in the code to see how its value went past the end of the array.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: Creating a 4x4 gird

    Quote Originally Posted by Norm View Post
    At line 49 the program used an index past the end of the array. The valid indexes for an array range from 0 to the array's length-1. For example a 6 element array's indexes are 0 to 5.
    Look at line 49 and see what index variable had the invalid value and then backtrack in the code to see how its value went past the end of the array.
    So the error is linked too:
    'matrix = new matrix[][]'
    ............................^

    The first [] is the problem, I don't know how to fix this...

    The outputs are always saying that it extends the reach of the array 'what ever number is in the first []'
    Last edited by sithclone3; 02-07-2015 at 07:53 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Creating a GUI
    By HoustonsOwn88 in forum New To Java
    Replies: 6
    Last Post: 11-16-2012, 09:38 AM
  2. Creating an App
    By Joao Lourenco in forum New To Java
    Replies: 2
    Last Post: 09-19-2012, 11:29 PM
  3. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  4. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  5. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM

Posting Permissions

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