Page 3 of 3 First 123
  • Jump to page:
    #31
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Can you provide an example?

    When i read in a line, i get lost at how to split up the values.
    So going from ####### to # # # # # # # .. Once i get that down, then i can add them to the array, while searching for the *.
  2. #32
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    That was one of the choices: separate the input characters in the file instead of having them all in one String.

    When i read in a line, i get lost at how to split up the values.
    Write a small simple program to work out the details.
    Define a String: String aStr = "ABCDEF";
    then write a for loop to loop for the length of the String
    Get the next character from the String using
    either substring() or charAt()
    print out the character
    end of loop

    Use the loop index as arg to the method to get the next char.
    Last edited by NormR; November 25th, 2012 at 09:28 PM.
  4. #33
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    So i figured out, how to read in the file. However, im not reading in another other rows, only the first line.

    Code:
     
    for(int x = 0; x < testing.length ; x++ ) 		 
    { 			 
    for(int y = 0; y < testing[x].length ; y++) 			 
    { 				
    testing[x][y] = value.charAt(y); 
    //testing[x][y] = value.charAt(y); 				
    
    if(testing[x][y] == '*'){ 					
    System.out.println("found it! :)"); 				    
    a = x; 				    
    b = y; 				   
    System.out.println("(" + a + "," + b + ")"); 				   
     break; 				
    } 				
    System.out.print(testing[x][y]); 	         } 			 			
    }
    Results....
    Code:
    Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION: 
    test 
    ############################################################
    So, it reads in the first line of code from the file (10 #'s), then it repeats 6 times (since its 6 10).

    However it suppose to read in the first row. Then insert it into the 2d array. Then go to the next row and do the same, etc...
  6. #34
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You should print out the full contents of the array after the code has read the file and saved its contents in the array so you can see if the code is working correctly.

    im not reading in another other rows, only the first line.
    Why not read all the lines in the file? See my pseudo code in post #30 for the steps that the code should take.
  8. #35
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    You should print out the full contents of the array after the code has read the file and saved its contents in the array so you can see if the code is working correctly.


    Why not read all the lines in the file? See my pseudo code in post #30 for the steps that the code should take.
    Alright, so i figured out how to loop through each value..

    1) I loop through each row then add it to a value..
    (So the first row (########) gets put in a object)

    2)I run a for loop, which splits each value up...
    (So since the first row have 10 values, then it will be ran 10 times. Using substring(i,i+1) to split up each value.)

    3) I convert it into a char primitive then store it in the array. It find the * now.

    However, i can't set the x,y properly. No matter what happens, the x value will always be 0, and once the * has been found , then it will simply make the y value 1. (0 for false, 1 for true).

    Code:
    char[][] testing = new char[rows][cols];
    		char d = 0;
    		int v = 0;
    		
    		String value;
    	//Error down here.
    		for(int x = 0; x < testing.length ; x++ ) 		 
    		{ 			 
    		for(int y = 0; y < testing[x].length ; y++) 			 
    		{ 				
    		 
    		if(test = in.hasNext()) 				 
    		{ 				
    		
    		value = in.next().toUpperCase();
    		
    		for(int i = 0; i < value.length();i++)
    		{
    			d = value.substring(i, i + 1).charAt(0);
    			testing[x][y] = d;
    			System.out.print(testing[x][y]);
    			if(d == '*')
    			{
    				 v = y;
    			}
    		}
    		System.out.println(" ");
    		System.out.println(v);
    		
    		
    		 } 			} 		 }
    Output..
    Code:
    ########## 
    0
    #OOOO*#O## 
    1
    #O####OOO# 
    1
    #O#O#OOOO# 
    1
    #OOOOO##OO 
    1
    ########## 
    1
  10. #36
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What is printed out for the whole of the testing array using the deepToString() method after all of the file has been read?
    Can you post the contents of the input file and what is printed out for the full contents after it is read and loaded into the array?

    What you have posted does not show the complete contents of the array.

    What is the code in post #35 supposed to do?
    Code:
    d = value.substring(i, i + 1).charAt(0);
    Why both the substring AND the charAt? There should only be one needed.

    Can you make a small program that reads the file and stores it in the array for testing. The posted code can not be compiled and executed for testing.
    Last edited by NormR; November 26th, 2012 at 10:08 AM.
  12. #37
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    What is printed out for the whole of the testing array using the deepToString() method after all of the file has been read?
    Can you post the contents of the input file and what is printed out for the full contents after it is read and loaded into the array?

    What you have posted does not show the complete contents of the array.

    What is the code in post #35 supposed to do?
    Code:
    d = value.substring(i, i + 1).charAt(0);
    Why both the substring AND the charAt? There should only be one needed.

    Can you make a small program that reads the file and stores it in the array for testing. The posted code can not be compiled and executed for testing.
    Well im trying to just store in each value of the file, rather than the whole row. In my old cold, the array would just store the whole row rather than each value in the file.

    I have to find the x,y of where the file first see '*', then i have to write a method, that will check the neighbor of the * to see if there is an open space, which then i would simply keep checking the neighbor space until i finish the maze.

    DeeptoString Output...
    Code:
    [[#,null,null,etc...]] //and it wont let me copy and paste, however, its clearly wrong. There are too many null spaces.
    Input File
    Code:
    6 10
    ##########
    #OOOO*#O##
    #O####OOO#
    #O#O#OOOO#
    #OOOOO##OO
    ##########
    Program Output after it reads to the array
    Code:
    ########## 
    #OOOO*#O## 
    #O####OOO# 
    #O#O#OOOO# 
    #OOOOO##OO 
    ##########
    The substring and char...
    Well my array have to be a char array, therefore i input the file into a string primitive, then i convert it over into a char. I use substring, so i can get each value rather than a whole line.

    Program Code...
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Assignment6 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		Scanner ask = new Scanner(System.in);
    		String ask_file;
    		
    		System.out.println("Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION: ");
    		ask_file = ask.nextLine();
    		
    		File inputFile = new File(ask_file + ".txt");
    		Scanner in = null;
    		
    		try {
    		 in = new Scanner(inputFile);
    		
    		 Integer rows = in.nextInt();
    		 Integer cols = in.nextInt();
    		 String maze = null;
    		 int a = 0, b = 0;
    		 boolean test;
    		
    		char[][] testing = new char[rows][cols];
    		char d = 0;
    		int v = 0;
    		
    		String value;
    	//Error down here.
    		for(int x = 0; x < testing.length ; x++ ) 		 
    		{ 			 
    		for(int y = 0; y < testing[x].length ; y++) 			 
    		{ 				
    		 
    		if(test = in.hasNext()) 				 
    		{ 				
    		
    		value = in.next().toUpperCase();
    		
    		for(int i = 0; i < value.length();i++)
    		{
    			d = value.substring(i, i + 1).charAt(0);
    			testing[x][y] = d;
    			System.out.print(testing[x][y] + " (" + (y + 1) + "," + (x + 1) + ") ");
    			if(d == '*')
    			{
    				 v = y;
    			}
    		}
    		System.out.println(" ");
    		//System.out.println(v);
    		
    		
    		 } 			} 		 }
    	        
    		 
    		 
    		 in.close();
    		} 
    		
    		catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Can not find file!");
    		}
    
    	}
    
    }
    Output..
    Code:
    Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION: 
    test
    # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1)  
    # (2,1) O (2,1) O (2,1) O (2,1) O (2,1) * (2,1) # (2,1) O (2,1) # (2,1) # (2,1)  
    # (3,1) O (3,1) # (3,1) # (3,1) # (3,1) # (3,1) O (3,1) O (3,1) O (3,1) # (3,1)  
    # (4,1) O (4,1) # (4,1) O (4,1) # (4,1) O (4,1) O (4,1) O (4,1) O (4,1) # (4,1)  
    # (5,1) O (5,1) O (5,1) O (5,1) O (5,1) O (5,1) # (5,1) # (5,1) O (5,1) O (5,1)  
    # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1)
    As if you notice, the y value should be the x value, also, the x value isn't working properly. It should be (1,1),(1,2)etc...
  14. #38
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Where does your posted code call the Arrays class's deepToString() method to show the contents of the two dim array?

    Why does the code do both substring and charAt() ??? Only one is needed.

    I don't understand why the code has 3 loops. There only needs to be two:
    The outer loop reads one line from the file and sets the index for the row in the 2D array
    The inner loop goes through the characters in that line one by one and assigns them to the columns in the current row.
    Last edited by NormR; November 26th, 2012 at 07:27 PM.
  16. #39
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    Where does your posted code call the Arrays class's deepToString() method to show the contents of the two dim array?

    Why does the code do both substring and charAt() ??? Only one is needed.

    I don't understand why the code has 3 loops. There only needs to be two:
    The outer loop reads one line from the file and sets the index for the row in the 2D array
    The inner loop goes through the characters in that line one by one and assigns them to the columns in the current row.
    I decided to just a smaller program, that inputs any value... which works fine!
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Assignment6 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		Scanner ask = new Scanner(System.in);
    		String ask_file;
    		
    		System.out.println("Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION: ");
    		ask_file = ask.nextLine();
    		
    		File inputFile = new File(ask_file + ".txt");
    		Scanner in = null;
    		
    		try {
    		 in = new Scanner(inputFile);
    		
    		 Integer rows = in.nextInt();
    		 Integer cols = in.nextInt();
    	     char row;
    		 int a = 0, b = 0;
    		 boolean test;
    		
    		char[][] testing = new char[rows][cols];
    		
    		
    		String value;
    	//Error down here.
    		for(int x = 0; x < testing.length ; x++ ) 		 
    		{ 			 
    		    for(int y = 0; y < testing[x].length ; y++) 			 
    		    { 				
    		       
    		    	a = x + 1;
    		    	b = y + 1;
    		    	
    		    	System.out.print("Enter the value in row " + a + " col " + b +": ");
    				testing[x][y] = ask.next().charAt(0);
    				
    				
    			System.out.println(a + " " + b);
    		            	
    		    } 	//End for y	 
    		}   //End for x
    	        
    		 
    		 
    		 in.close();
    		} 
    		
    		catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Can not find file!");
    		}
    
    	}
    
    }
    So, how can i do this correctly, with using a inputted file? Like how can i make the program input the values in each role, then go down and do it again?

    (So instead of my manually inputting the code, like the program above, it does it by it self.)

    And...
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Assignment6 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		Scanner ask = new Scanner(System.in);
    		String ask_file;
    		
    		System.out.println("Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION: ");
    		ask_file = ask.nextLine();
    		
    		File inputFile = new File(ask_file + ".txt");
    		Scanner in = null;
    		
    		try {
    		 in = new Scanner(inputFile);
    		
    		 Integer rows = in.nextInt();
    		 Integer cols = in.nextInt();
    		 String maze = null;
    		 int a = 0, b = 0;
    		 boolean test;
    		
    		char[][] testing = new char[rows][cols];
    		char d = 0;
    		int v = 0;
    		
    		String value;
    	//Error down here.
    		for(int x = 0; x < testing.length ; x++ ) 		 
    		{ 			 
    		for(int y = 0; y < testing[x].length ; y++) 			 
    		{ 				
    		 
    		if(test = in.hasNext()) 				 
    		{ 				
    		
    		value = in.next().toUpperCase();
    		
    		for(int i = 0; i < value.length();i++)
    		{
    			d = value.substring(i, i + 1).charAt(0);
    			testing[x][y] = d;
    			System.out.print(Arrays.deepToString(testing));
    			if(d == '*')
    			{
    				 v = y;
    			}
    		}
    		System.out.println(" ");
    		//System.out.println(v);
    		
    		
    		 } 			} 		 }
    	        
    		 
    		 
    		 in.close();
    		} 
    		
    		catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Can not find file!");
    		}
    
    	}
    
    }
  18. #40
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You need to read the line into the value variable in the outer loop with the for(x)
    and extract the characters and save them one by one in the inner loop with for(y)

    Get rid of the third loop with for(i)

    Get rid of the use of substring(), only charAt() is needed
    Last edited by NormR; November 26th, 2012 at 07:56 PM.
  20. #41
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    You need to read the line into the value variable in the outer loop with the for(x)
    and extract the characters and save them one by one in the inner loop with for(y)

    Get rid of the third loop with for(i)

    Get rid of the use of substring(), only charAt() is needed
    Thank you! I finally got the code working.
  22. #42
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Location
    Tempe
    Posts
    38
    Rep Power
    3
    Problem Solved
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo