The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Page 2 -
Homework - Reading in a file Help
Page 2 - Discuss Reading in a file Help in the Java Help forum on Dev Shed. Reading in a file Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 25th, 2012, 04:37 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Please post the full text of the error message. Each post has an id number in the upper right. Where is the error message?
|

November 25th, 2012, 05:06 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR Please post the full text of the error message. Each post has an id number in the upper right. Where is the error message? | Quote: | 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)
|

November 25th, 2012, 05:19 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Assignment6.main(Assignment6.java:36) |
On line 36 the program calls the next() method when there is nothing to be read.
If you use the hasNext() method you would be able to tell if there was something to be read before calling next() to read it.
|

November 25th, 2012, 05:35 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
|
Ok, fixed.
Now what about the issue when finding the starting point? It should say (2,6) however, nothing comes up.
|

November 25th, 2012, 05:52 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: | issue when finding the starting point? It should say (2,6) however, nothing comes up. |
Sorry, I don't know what you are talking about. Please explain in more detail.
Post the program's output, explain what is wrong with it and show what it should be.
|

November 25th, 2012, 06:06 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
Ok.. So im trying to find when the program finds "*".
For debuggin reasons, i have it on where if the program find the *, then simple output "Found it."
However, i am always getting "Do not see it.".
What is happening, is that the program is comparing the whole row rather then each individual value. For example, the first row is nothing but #'s, and instead of comparing the # to *, it compares the whole line of ######## to *.
Code:
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
if(test = in.hasNext())
{
testing[x][y] = in.next().toUpperCase();
//Output the maze
System.out.print(testing[x][y]);
if(testing[x][y].equals("*"))
System.out.println("Found it! :)");
else
System.out.println("Do not find it.");
} } }
Output.
Code:
##########Do not find it.
#OOOO*#O##Do not find it.
#O####OOO#Do not find it.
#O#O#OOOO#Do not find it.
#OOOOO##OODo not find it.
##########Do not find it.
What is it doing....
Comparing (##########) to (*)
Comparing (#OOOO*#O##) to (*), etc...
What i want happening....
Comparing(#) to (*) ...
|

November 25th, 2012, 06:30 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
[QUOTE]Comparing (##########) to (*)
Did you read and save the ##########s instead of saving individual #s?
Can you change the input file so that the #s are read separately
Or change the code to split the String of #######s to separate #s
When you read in the data from the file, if you would print out each item that is read you would see what the code had read:
########## vs #
|

November 25th, 2012, 06:44 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
[QUOTE=NormR] Quote: Comparing (##########) to (*)
Did you read and save the ##########s instead of saving individual #s?
Can you change the input file so that the #s are read separately
Or change the code to split the String of #######s to separate #s
When you read in the data from the file, if you would print out each item that is read you would see what the code had read:
########## vs # |
How would i split the code? Would in.usedelimiter be how to do it? Because when i tried to use substring, it only output the first 6 values.
|

November 25th, 2012, 06:47 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: | How would i split the code |
I assume you mean to separate the ##### into separate characters: # # # # #
The String class's substring method would do that to separate Strings or the charAt() to separate chars
|

November 25th, 2012, 06:51 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR I assume you mean to separate the ##### into separate characters: # # # # #
The String class's substring method would do that to separate Strings or the charAt() to separate chars |
Yea, but what is happening is,:
Code:
#
Do not find it.
#
Do not find it.
#
Do not find it.
#
Do not find it.
#
Do not find it.
#
Do not find it.
Code change..
Code:
maze = testing[x][y].substring(0,1);
|

November 25th, 2012, 06:57 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Try working on the code to load the array and then print out the array's contents to see what you have loaded. Use the Arrays class's deepToString() method to format the contents of the array for printing.
Code:
String[][] twoD = {{"a", "b"}, {"c", "d", "E"}};
System.out.println(Arrays.deepToString(twoD)); // [[a, b], [c, d, E]]
Post the code for loading the array if you have problems.
|

November 25th, 2012, 07:07 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
I get...
Code:
[[##########, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
[[##########, #OOOO*#O##, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
[[##########, #OOOO*#O##, #O####OOO#, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
[[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
[[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, #OOOOO##OO, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
[[##########, #OOOO*#O##, #O####OOO#, #O#O#OOOO#, #OOOOO##OO, ##########, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null], [null, null, null, null, null, null, null, null, null, null]]
|

November 25th, 2012, 07:15 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
That is obviously wrong. I assume the array should contain single char/Strings in each slot.
You need to
either change the input file so there are single characters in it instead of Strings of characters
or change the code that reads the long Strings of characters to separate them into single characters by using substring() or charAt()
Work on the maze loading method to put single characters into each slot in the array,
Last edited by NormR : November 25th, 2012 at 07:18 PM.
|

November 25th, 2012, 07:51 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by NormR That is obviously wrong. I assume the array should contain single char/Strings in each slot.
You need to
either change the input file so there are single characters in it instead of Strings of characters
or change the code that reads the long Strings of characters to separate them into single characters by using substring() or charAt()
Work on the maze loading method to put single characters into each slot in the array, |
1) If i was going to do the substring.. I would first use the substring to separate all the values, then insert them into the array?
2) If i was going to use charAt(), wouldnt i have to convert the array in a char array? Also, how would i be able to insert ALL the values into the array? Because the code i have only insert ONE value per each row into the array..
So if i do testing[x][y] = in.next().charAt(0); then it would only insert the first value per line. Which makes sense, since i didnt do a loop, but how would i do a loop, so first it in.next.charAt(0), then charAt(1). , then so on. I cant figure it out.
|

November 25th, 2012, 08:02 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Do it in separate steps:
begin loop thru the lines in the file
Read the line of characters for the current row from the file
Begin loop through the characters in the line read
get the next character from the line read
save the character in the array
increment the index to the next slot in the array for this row
end loop through the characters
increment index to the next row in the array
end loop through the lines in the file
Last edited by NormR : November 25th, 2012 at 08:04 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|