How can I return this array to a method with the file parameter?
up vote
-1
down vote
favorite
I'm doing a project right now, in which will be checking a text file (which is battleship) and reading a grid into an array. The text file starts with the size of the grid on the first line, then the number of ships and all of the lengths following that first number on the second line, and the grid on all of the following lines. This grid is going to be changing for multiple test cases.
I am attempting to return an an array from a method (generateGrid) whose parameters are of (Scanner, int) into a method (checkGrid) that has the parameter of (File). This does not work, and eclipse tells me I need to change one of these parameters.
Attached is the method that checks the grid, the method that generates an array from a text file, and the text file itself.
Here's the info from the text file. In the file it isn't separated by lines normally but I couldn't figure out how to format it on here so that every number wasn't next to each other, so try and imagine that the text file lines don't actually have full lines as the delimiter, just spaces in between.
I debugged and fixed the boolean issue (removed from the post now). Thanks for the comment about System.out.print! I actually already know how to debug, so that was no help at all.
8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
//method that reads the text file into an array
public static int generateGrid(Scanner readFile, int gridSize) {
//creates a number to count the lines of the text file, starts at negative two because the
//text file starts with two lines that don't contain the grid
int dimNum = 0;
while (readFile.hasNextLine()) {
++dimNum;
readFile.nextLine();
}
int gridArray = {};
for (int i = 3; i <= dimNum; ++i ) {
for (int j = 0; j <= dimNum; ++j) {
gridArray[i][j] = readFile.nextInt();
}
}
return gridArray;
}
//this is the method that does the checks
public static boolean checkGrid(File input) throws FileNotFoundException {
//boolean statement to return
boolean validCheck = true;
Scanner scanCheck = new Scanner(input);
//gridSize, takes the first int in the text file
int gridSize = scanCheck.nextInt();
//clears rest of the line for the scanner
scanCheck.nextLine();
//numShips takes the second num in the file
int numShips = scanCheck.nextInt();
int shipLengths = new int[numShips];
while ((scanCheck.nextInt() != 0) || (scanCheck.nextInt() != 1)) {
for (int i = 1; i < 2; ++i) {
shipLengths[i] = scanCheck.nextInt();
if (shipLengths[i] < 2 || shipLengths[i] > 6) {
validCheck = false;
}
else {
}
}
}
return validCheck;
java arrays method-call
add a comment |
up vote
-1
down vote
favorite
I'm doing a project right now, in which will be checking a text file (which is battleship) and reading a grid into an array. The text file starts with the size of the grid on the first line, then the number of ships and all of the lengths following that first number on the second line, and the grid on all of the following lines. This grid is going to be changing for multiple test cases.
I am attempting to return an an array from a method (generateGrid) whose parameters are of (Scanner, int) into a method (checkGrid) that has the parameter of (File). This does not work, and eclipse tells me I need to change one of these parameters.
Attached is the method that checks the grid, the method that generates an array from a text file, and the text file itself.
Here's the info from the text file. In the file it isn't separated by lines normally but I couldn't figure out how to format it on here so that every number wasn't next to each other, so try and imagine that the text file lines don't actually have full lines as the delimiter, just spaces in between.
I debugged and fixed the boolean issue (removed from the post now). Thanks for the comment about System.out.print! I actually already know how to debug, so that was no help at all.
8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
//method that reads the text file into an array
public static int generateGrid(Scanner readFile, int gridSize) {
//creates a number to count the lines of the text file, starts at negative two because the
//text file starts with two lines that don't contain the grid
int dimNum = 0;
while (readFile.hasNextLine()) {
++dimNum;
readFile.nextLine();
}
int gridArray = {};
for (int i = 3; i <= dimNum; ++i ) {
for (int j = 0; j <= dimNum; ++j) {
gridArray[i][j] = readFile.nextInt();
}
}
return gridArray;
}
//this is the method that does the checks
public static boolean checkGrid(File input) throws FileNotFoundException {
//boolean statement to return
boolean validCheck = true;
Scanner scanCheck = new Scanner(input);
//gridSize, takes the first int in the text file
int gridSize = scanCheck.nextInt();
//clears rest of the line for the scanner
scanCheck.nextLine();
//numShips takes the second num in the file
int numShips = scanCheck.nextInt();
int shipLengths = new int[numShips];
while ((scanCheck.nextInt() != 0) || (scanCheck.nextInt() != 1)) {
for (int i = 1; i < 2; ++i) {
shipLengths[i] = scanCheck.nextInt();
if (shipLengths[i] < 2 || shipLengths[i] > 6) {
validCheck = false;
}
else {
}
}
}
return validCheck;
java arrays method-call
1
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm doing a project right now, in which will be checking a text file (which is battleship) and reading a grid into an array. The text file starts with the size of the grid on the first line, then the number of ships and all of the lengths following that first number on the second line, and the grid on all of the following lines. This grid is going to be changing for multiple test cases.
I am attempting to return an an array from a method (generateGrid) whose parameters are of (Scanner, int) into a method (checkGrid) that has the parameter of (File). This does not work, and eclipse tells me I need to change one of these parameters.
Attached is the method that checks the grid, the method that generates an array from a text file, and the text file itself.
Here's the info from the text file. In the file it isn't separated by lines normally but I couldn't figure out how to format it on here so that every number wasn't next to each other, so try and imagine that the text file lines don't actually have full lines as the delimiter, just spaces in between.
I debugged and fixed the boolean issue (removed from the post now). Thanks for the comment about System.out.print! I actually already know how to debug, so that was no help at all.
8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
//method that reads the text file into an array
public static int generateGrid(Scanner readFile, int gridSize) {
//creates a number to count the lines of the text file, starts at negative two because the
//text file starts with two lines that don't contain the grid
int dimNum = 0;
while (readFile.hasNextLine()) {
++dimNum;
readFile.nextLine();
}
int gridArray = {};
for (int i = 3; i <= dimNum; ++i ) {
for (int j = 0; j <= dimNum; ++j) {
gridArray[i][j] = readFile.nextInt();
}
}
return gridArray;
}
//this is the method that does the checks
public static boolean checkGrid(File input) throws FileNotFoundException {
//boolean statement to return
boolean validCheck = true;
Scanner scanCheck = new Scanner(input);
//gridSize, takes the first int in the text file
int gridSize = scanCheck.nextInt();
//clears rest of the line for the scanner
scanCheck.nextLine();
//numShips takes the second num in the file
int numShips = scanCheck.nextInt();
int shipLengths = new int[numShips];
while ((scanCheck.nextInt() != 0) || (scanCheck.nextInt() != 1)) {
for (int i = 1; i < 2; ++i) {
shipLengths[i] = scanCheck.nextInt();
if (shipLengths[i] < 2 || shipLengths[i] > 6) {
validCheck = false;
}
else {
}
}
}
return validCheck;
java arrays method-call
I'm doing a project right now, in which will be checking a text file (which is battleship) and reading a grid into an array. The text file starts with the size of the grid on the first line, then the number of ships and all of the lengths following that first number on the second line, and the grid on all of the following lines. This grid is going to be changing for multiple test cases.
I am attempting to return an an array from a method (generateGrid) whose parameters are of (Scanner, int) into a method (checkGrid) that has the parameter of (File). This does not work, and eclipse tells me I need to change one of these parameters.
Attached is the method that checks the grid, the method that generates an array from a text file, and the text file itself.
Here's the info from the text file. In the file it isn't separated by lines normally but I couldn't figure out how to format it on here so that every number wasn't next to each other, so try and imagine that the text file lines don't actually have full lines as the delimiter, just spaces in between.
I debugged and fixed the boolean issue (removed from the post now). Thanks for the comment about System.out.print! I actually already know how to debug, so that was no help at all.
8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
//method that reads the text file into an array
public static int generateGrid(Scanner readFile, int gridSize) {
//creates a number to count the lines of the text file, starts at negative two because the
//text file starts with two lines that don't contain the grid
int dimNum = 0;
while (readFile.hasNextLine()) {
++dimNum;
readFile.nextLine();
}
int gridArray = {};
for (int i = 3; i <= dimNum; ++i ) {
for (int j = 0; j <= dimNum; ++j) {
gridArray[i][j] = readFile.nextInt();
}
}
return gridArray;
}
//this is the method that does the checks
public static boolean checkGrid(File input) throws FileNotFoundException {
//boolean statement to return
boolean validCheck = true;
Scanner scanCheck = new Scanner(input);
//gridSize, takes the first int in the text file
int gridSize = scanCheck.nextInt();
//clears rest of the line for the scanner
scanCheck.nextLine();
//numShips takes the second num in the file
int numShips = scanCheck.nextInt();
int shipLengths = new int[numShips];
while ((scanCheck.nextInt() != 0) || (scanCheck.nextInt() != 1)) {
for (int i = 1; i < 2; ++i) {
shipLengths[i] = scanCheck.nextInt();
if (shipLengths[i] < 2 || shipLengths[i] > 6) {
validCheck = false;
}
else {
}
}
}
return validCheck;
java arrays method-call
java arrays method-call
edited 2 days ago
asked 2 days ago
Jrobb
11
11
1
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago
add a comment |
1
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago
1
1
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402773%2fhow-can-i-return-this-array-to-a-method-with-the-file-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Stack Overflow! It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
2 days ago
System.out.println() is your friend. Use it everywhere to find out what your variables contain as opposed to what you think they should be.
– Perdi Estaquel
2 days ago