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

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0

    Reading in a file Help


    Hello,

    I'm having trouble from reading in a file. The file's first line will contain two numbers(int int), in which i would have to use those numbers in a 2D array. Below the two numbers will be a maze.

    The main problem that i am having is that i can not read the two numbers in.

    I'd tried using Scanner and BufferedReader, but i would just get Hello World, errors, or a very long number.

    Any help please?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    i can not read the two numbers in.
    Can you post the code you are using to read the numbers?
    I assume the numbers are text (Strings) and need to be converted to int values in the program.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Well i figured out the error.

    However the next issue is reading the maze..

    To clear it up, the file look like this:
    5 5
    #####
    #XOO#
    ###O#
    OOOO#
    #*###

    Where the '*' represents the starting point and the X represents the ending point.

    I know that i have to use a 2d array, but im having trouble in this error. Instead of
    Code:
    boolean maze[][]= new boolean[5][5] //Or [rows][cols]
    Its giving me an error. My code is not complete at all, and i havent even started the complicated part with solving the actual maze.

    But here is what i have so far..
    Main file
    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); 		 		 
    //List all the values into the arraylist 		 
    ArrayList<String> values = new ArrayList<String>(); 		 
    Integer rows = null; 		 
    Integer cols = null; 		  		 
    
    in.useDelimiter(" "); 		
    //Add the values into the arraylist 		 
    while(in.hasNext()) 		 
    { 			
    rows = in.nextInt(); 			 
    cols = in.nextInt(); 			 
    values.add(in.nextLine()); 		 
    } 		  		 
    
    boolean maze[][] = new boolean[rows][cols]; 		  		
    in.close(); 		
    }  		 		
    catch (FileNotFoundException e) { 			
    // TODO Auto-generated catch block 			
    System.out.println("Can not find file!"); 		
    }  	
    }  
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Its giving me an error
    Please post the full text of any error messages you have questions about.
  8. #5
  9. 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 any error messages you have questions about.
    Code:
    Exception in thread "main" java.util.InputMismatchException 	at java.util.Scanner.throwFor(Unknown Source) 	
    at java.util.Scanner.next(Unknown Source) 	
    at java.util.Scanner.nextInt(Unknown Source) 	
    at java.util.Scanner.nextInt(Unknown Source) 	
    at Assignment6.main(Assignment6.java:33)
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    At line 33 the program is calling the nextInt() method to read in a number and convert it to an int value. The data being read is not numeric and can't be converted to an int.
    Check the data in the file that is being read and make sure it is numeric.

    To see what is being read, add some println statements that print out everything that is read as it is read so you can see where in the file the call to nextInt() is going wrong.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Alright, i fix that error, but now im getting this:

    Code:
    Exception in thread "main" java.util.InputMismatchException 	
    at java.util.Scanner.throwFor(Unknown Source) 	
    at java.util.Scanner.next(Unknown Source) 	
    at java.util.Scanner.nextInt(Unknown Source) 	
    at java.util.Scanner.nextInt(Unknown Source) 	
    at Assignment6.main(Assignment6.java:32)
    I'd change my code a little, but this is where the error is coming from:
    Code:
    Integer rows = null; 		 
    Integer cols = null; 		
    String maze = null; 		  		
    
    in.useDelimiter(" "); 				 
    while(in.hasNext()) 		
    { 			 
    rows = in.nextInt(); 			 
    cols = in.nextInt(); 		//Line 32	 
    maze = in.next();  		 
    }
    FILE:
    5 5
    #####
    OOOO#
    O####
    OOOO#
    ###*#

    As where im trying to get a number value for rows and cols, and then the actually maze will go into the maze values(debugging reasons).

    Now, the rows value works, however, when it goes to input the number into the cols value, it puts the number and the maze, which is generating the error. I tried changing the delimiter so its (" |\\n"), but then i would get rows = X###### and cols=O#####.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Why does the code read the rows and cols inside of the loop?
    Does each line begin with two numbers? The sample file posted looks like there is only one line with two numbers at the beginning of the file.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    Why does the code read the rows and cols inside of the loop?
    Does each line begin with two numbers? The sample file posted looks like there is only one line with two numbers at the beginning of the file.
    Ok, that was it. Im new to the whole inputting files thing.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    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.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    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)
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    better to use a string array or a char array?
    Depends what you are going to do with the contents. Strings are objects and require methods to work with. char are primitives and can be used with arithmetic operators like ==, <, > etc
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    Depends what you are going to do with the contents. Strings are objects and require methods to work with. char are primitives and can be used with arithmetic operators like ==, <, > etc
    Well, i have to create a class, that does the actual part and for the constructor it have to be...
    public mazesolver(char [][] input, int rows, int cols)...
    and im extremely confused on why i need parameter of char 2d array. Also, i no idea on why i would use it.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    why i need parameter of char 2d array
    That will be the maze that will be searched.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by NormR
    That will be the maze that will be searched.
    Ok, and can you help me with my error... i edited one of my posts.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo