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;









share|improve this question




















  • 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















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;









share|improve this question




















  • 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













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;









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Sphinx de Gizeh