#16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    help me with my error
    Please post the full text of the error message. Each post has an id number in the upper right. Where is the error message?
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    Please post the full text of the error message. Each post has an id number in the upper right. Where is the error message?
    Originally Posted by NormR
    You need to think about what sequence program statements are executed in. Nothing to do with reading from a file. Everything to do with looping.
    Ok thanks. now this is the tricky part. Im having trouble with searching through the maze to find the starting point. When i tried storing all the files into an arraylist, however, i need the x and the y point, so using an arraylist isnt the smartest idea.

    Therefore i would need to use a 2D array. Would it be better to use a string array or a char array?

    Secondly, here is what i have up to par.
    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; 		 		
    
    String[][] testing = new String[rows][cols]; 	      		  	
    //Error down here... 		 
    for(int x = 0; x < testing.length ; x++ ) 		 
    { 			
     for(int y = 0; y < testing[x].length ; y++) 			 
    { 				
    testing[x][y] = in.next();	 //Error here! 				
    System.out.println(testing[x][y]); 				 				
    //Doesnt work! 
    if(testing[x][y].equals("*")) 				{ 					
    a = x; 					
    b = y; 					
    System.out.println("(a,b) : " + " (" + a + "," + b + ")."); 					
    //break; 				
    } 			 
    } 		 
    } 		  		 
    
    in.close(); 		}  		 		
    
    catch (FileNotFoundException e) { 			
    // TODO Auto-generated catch block 			
    System.out.println("Can not find file!"); 		}  	}  }
    Error / Output...
    Code:
    ########## 
    Exception in thread "main" 
    #OOOO*#O## 
    #O####OOO# 
    #O#O#OOOO# 
    #OOOOO##OO 
    ########## 
    java.util.NoSuchElementException 	
    at java.util.Scanner.throwFor(Unknown Source) 	
    at java.util.Scanner.next(Unknown Source) 	
    at Assignment6.main(Assignment6.java:36)
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at Assignment6.main(Assignment6.java:36)
    On line 36 the program calls the next() method when there is nothing to be read.
    If you use the hasNext() method you would be able to tell if there was something to be read before calling next() to read it.
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Ok, fixed.

    Now what about the issue when finding the starting point? It should say (2,6) however, nothing comes up.
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    issue when finding the starting point? It should say (2,6) however, nothing comes up.
    Sorry, I don't know what you are talking about. Please explain in more detail.

    Post the program's output, explain what is wrong with it and show what it should be.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Ok.. So im trying to find when the program finds "*".

    For debuggin reasons, i have it on where if the program find the *, then simple output "Found it."

    However, i am always getting "Do not see it.".

    What is happening, is that the program is comparing the whole row rather then each individual value. For example, the first row is nothing but #'s, and instead of comparing the # to *, it compares the whole line of ######## to *.

    Code:
    for(int x = 0; x < testing.length ; x++ ) 		 
    { 			 
    for(int y = 0; y < testing[x].length ; y++) 			 
    { 				
     if(test = in.hasNext()) 				 
    { 				
    testing[x][y] = in.next().toUpperCase();	 
    //Output the maze	
    System.out.print(testing[x][y]); 	
    
    			 				
    if(testing[x][y].equals("*"))	 					
    System.out.println("Found it! :)"); 				
    
    else 					
    System.out.println("Do not find it."); 			 			
     } 			} 		 }
    Output.
    Code:
    ##########Do not find it. 
    #OOOO*#O##Do not find it. 
    #O####OOO#Do not find it. 
    #O#O#OOOO#Do not find it. 
    #OOOOO##OODo not find it. 
    ##########Do not find it.
    What is it doing....
    Comparing (##########) to (*)
    Comparing (#OOOO*#O##) to (*), etc...

    What i want happening....
    Comparing(#) to (*) ...
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    [QUOTE]Comparing (##########) to (*)
    Did you read and save the ##########s instead of saving individual #s?

    Can you change the input file so that the #s are read separately
    Or change the code to split the String of #######s to separate #s

    When you read in the data from the file, if you would print out each item that is read you would see what the code had read:
    ########## vs #
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    [QUOTE=NormR]
    Comparing (##########) to (*)
    Did you read and save the ##########s instead of saving individual #s?

    Can you change the input file so that the #s are read separately
    Or change the code to split the String of #######s to separate #s

    When you read in the data from the file, if you would print out each item that is read you would see what the code had read:
    ########## vs #
    How would i split the code? Would in.usedelimiter be how to do it? Because when i tried to use substring, it only output the first 6 values.
  16. #24
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How would i split the code
    I assume you mean to separate the ##### into separate characters: # # # # #

    The String class's substring method would do that to separate Strings or the charAt() to separate chars
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    I assume you mean to separate the ##### into separate characters: # # # # #

    The String class's substring method would do that to separate Strings or the charAt() to separate chars
    Yea, but what is happening is,:
    Code:
    # 
    Do not find it. 
    #
    Do not find it.
    # 
    Do not find it. 
    # 
    Do not find it. 
    #
    Do not find it. 
    # 
    Do not find it.
    Code change..
    Code:
    maze = testing[x][y].substring(0,1);
  20. #26
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Try working on the code to load the array and then print out the array's contents to see what you have loaded. Use the Arrays class's deepToString() method to format the contents of the array for printing.
    Code:
          String[][] twoD = {{"a", "b"}, {"c", "d", "E"}};
          System.out.println(Arrays.deepToString(twoD)); // [[a, b], [c, d, E]]
    Post the code for loading the array if you have problems.
  22. #27
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    I get...
    Code:
    [[##########, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]] 
    [[##########, #OOOO*#O##, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]] 
    [[##########, #OOOO*#O##, #O####OOO#, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
     [[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]] 
    [[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, #OOOOO##OO, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]] 
    [[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, #OOOOO##OO, ##########, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
  24. #28
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    That is obviously wrong. I assume the array should contain single char/Strings in each slot.

    You need to
    either change the input file so there are single characters in it instead of Strings of characters
    or change the code that reads the long Strings of characters to separate them into single characters by using substring() or charAt()

    Work on the maze loading method to put single characters into each slot in the array,
    Last edited by NormR; November 25th, 2012 at 07:18 PM.
  26. #29
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    That is obviously wrong. I assume the array should contain single char/Strings in each slot.

    You need to
    either change the input file so there are single characters in it instead of Strings of characters
    or change the code that reads the long Strings of characters to separate them into single characters by using substring() or charAt()

    Work on the maze loading method to put single characters into each slot in the array,
    1) If i was going to do the substring.. I would first use the substring to separate all the values, then insert them into the array?

    2) If i was going to use charAt(), wouldnt i have to convert the array in a char array? Also, how would i be able to insert ALL the values into the array? Because the code i have only insert ONE value per each row into the array..

    So if i do testing[x][y] = in.next().charAt(0); then it would only insert the first value per line. Which makes sense, since i didnt do a loop, but how would i do a loop, so first it in.next.charAt(0), then charAt(1). , then so on. I cant figure it out.
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Do it in separate steps:
    begin loop thru the lines in the file
    Read the line of characters for the current row from the file

    Begin loop through the characters in the line read
    get the next character from the line read
    save the character in the array
    increment the index to the next slot in the array for this row
    end loop through the characters

    increment index to the next row in the array
    end loop through the lines in the file
    Last edited by NormR; November 25th, 2012 at 08:04 PM.

IMN logo majestic logo threadwatch logo seochat tools logo