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
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).
Later, for storing
Code Java:
FileWriter out = new FileWriter(file);
prop.setProperty("rows", rows+"");
prop.setProperty("cols", cols+"");
prop.store(out, "--No Comment--");
out.close();
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
Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP
Highlight your code with
Code :
[highlight=Java] and [/highlight]
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
Re: Using a scanner to input row an column sizes for my grid from a file PLEASE HELP
Code :
<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>