Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    Hello Im brand new to this forum I'm very new to java programming Im working on a programming exercise that I have to create a word search using a 2d array. I have 2 questions the first on being is I have to use the Scanner class to read in # of rows and cols then instantiate a char[][] of that size, and store it in grid then fill grid with random chars. Im not sure how to go about doing this as far as reading from a file an using that info to make the size of my array

    thnak you for your help


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    You could use properties. It's VERY basic, perfect when you just need to store 1-5 basic variables (EG, int, String, long, double).
    /*
     * Precondition -> file is a String to the loccation of the file,
     */
    FileReader fr = new FileReader(file);
    Properties prop = new Properties();
    prop.load(fr);
    int rows = Integer.valueOf(prop.get("rows"));
    int cols = Integer.valueOf(prop.get("cols"));

    Later, for storing
    FileWriter out = new FileWriter(file);
    prop.setProperty("rows", rows+"");
    prop.setProperty("cols", cols+"");
    prop.store(out, "--No Comment--");
    out.close();
    Last edited by Tjstretch; October 19th, 2011 at 06:32 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    thank you this helped I need help with placing my words in my grid using a switch statement , here is what I have thus far

    public class WordSearchlab3
    {



    public static void main(String[] args) throws IOException
    {
    generateWordSearchProblem("Instructions");

    }

    public static void generateWordSearchProblem(String filename)
    throws IOException
    {
    Scanner scan = new Scanner(new File(filename));

    char[][] grid;

    grid = makeGrid(scan);
    // placeWords(grid, scan);
    printGrid(grid);
    }

    public static char[][] makeGrid(Scanner scan) throws IOException
    {


    Scanner input = new Scanner(System.in);

    int r = scan.nextInt();
    int c = scan.nextInt(); // varibles

    char[][] grid = new char[r][c];

    for (int i = 0; i < grid.length; i++)
    {

    for (int c2 = 0; c2 < grid[i].length; c2++)
    {

    Random rng = new Random();
    char j = (char) (rng.nextInt(26) + 'a');

    grid [i][c2] =j ;

    }

    }

    return grid ;

    }
    public static void placeWords(char[][] grid, Scanner scan)
    {

    int startRow = scan.nextInt();
    int StartCol = scan.nextInt();

    String direction = scan.next();
    String word = scan.next();


    switch (direction.charAt(0)) {

    case 'N':

    case 'S':

    case 'E':

    case 'W':


    }







    // use the Scanner to read in each line of the text file
    // obtain the starting row, starting col, direction and word
    // use that to place the word in the grid
    }

    public static void printGrid(char[][] grid)


    {
    for (int i = 0; i < grid.length; i++)
    {
    for (int k = 0; k < grid[i].length; k++)
    {
    System.out.print(grid[i][k]);
    }
    System.out.print("\n");
    }
    }
    }

    This is the output of the sample I need 10 , 15 represents the size of the grid 0 0 HELLO is the direction Im trying to get these words implemented into my grid using a switch statement

    10 15
    0 0 S HELLO
    5 9 W GOODBYE
    3 2 E HELP
    would produce this grid:
    Huhksdoval
    Ethlrwayrz
    Lttxkkwlcu
    LsHELPpgyg
    Owylzoigvi
    tqhEYBDOOG
    kcqjrlalgj
    qbeqhnhjeh
    niqzjkyyir
    vtfdefbhjt

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    Highlight your code with
    [highlight=Java] and [/highlight]

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    I m not sure hot to do that sorry could you show me to do that

    thanks

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP

    <script type="text/javascript">
    <!--
       public class WordSearchlab3
    {
     
     
     
    	public static void main(String[] args) throws IOException
    	{
    		generateWordSearchProblem("Instructions");
     
    	}
     
    	public static void generateWordSearchProblem(String filename)
    		throws IOException
    	{
    		Scanner scan = new Scanner(new File(filename));
     
    		char[][] grid;
     
    		grid = makeGrid(scan);
    //		placeWords(grid, scan);
    		printGrid(grid);
    	}
     
    	public static char[][] makeGrid(Scanner scan) throws IOException
    	{
     
     
    		Scanner input = new Scanner(System.in);
     
    		int r = scan.nextInt();
    		int c = scan.nextInt();  // varibles 
     
    		char[][] grid = new char[r][c];
     
    		for (int i = 0; i < grid.length; i++)
    		{
     
    			for (int c2 = 0; c2 < grid[i].length; c2++)
    			{
     
    			Random rng = new Random();
    			char j = (char) (rng.nextInt(26) + 'a');
     
             grid [i][c2] =j ; 
     
    			}
     
    		}
     
    	    return grid ;
     
    	}
    	public static void placeWords(char[][] grid, Scanner scan)
    	{
     
    	    int startRow = scan.nextInt(); 
    		 int StartCol = scan.nextInt();
     
    		 String direction = scan.next(); 
    		 String word = scan.next();
     
     
    		 switch  (direction.charAt(0))     { 
     
    			 case 'N':  
     
    			 case 'S': 
     
    			 case 'E': 
     
    			 case 'W': 
     
     
    		}
     
     
     
     
     
     
     
    		// use the Scanner to read in each line of the text file
    		// obtain the starting row, starting col, direction and word
    		// use that to place the word in the grid
    	}
     
    	public static void printGrid(char[][] grid)
     
     
    	{
    		for (int i = 0; i < grid.length; i++)
    		{
    			for (int k = 0; k < grid[i].length; k++)
    			{
    				System.out.print(grid[i][k]);
    			}
    			System.out.print("\n");
    		}
    	}
    }
    //-->
    </script>

Similar Threads

  1. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  4. using Scanner for 75mb file
    By zort in forum Java SE APIs
    Replies: 5
    Last Post: November 9th, 2009, 04:20 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM