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 3 -
Homework - Reading in a file Help
Page 3 - 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, 08:12 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
|
Can you provide an example?
When i read in a line, i get lost at how to split up the values.
So going from ####### to # # # # # # # .. Once i get that down, then i can add them to the array, while searching for the *.
|

November 25th, 2012, 08:15 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
That was one of the choices: separate the input characters in the file instead of having them all in one String.
Quote: | When i read in a line, i get lost at how to split up the values. |
Write a small simple program to work out the details.
Define a String: String aStr = "ABCDEF";
then write a for loop to loop for the length of the String
Get the next character from the String using
either substring() or charAt()
print out the character
end of loop
Use the loop index as arg to the method to get the next char.
Last edited by NormR : November 25th, 2012 at 08:28 PM.
|

November 25th, 2012, 09:31 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
So i figured out, how to read in the file. However, im not reading in another other rows, only the first line.
Code:
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
testing[x][y] = value.charAt(y);
//testing[x][y] = value.charAt(y);
if(testing[x][y] == '*'){
System.out.println("found it! :)");
a = x;
b = y;
System.out.println("(" + a + "," + b + ")");
break;
}
System.out.print(testing[x][y]); }
}
Results....
Code:
Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION:
test
############################################################
So, it reads in the first line of code from the file (10 #'s), then it repeats 6 times (since its 6 10).
However it suppose to read in the first row. Then insert it into the 2d array. Then go to the next row and do the same, etc...
|

November 26th, 2012, 06:51 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
You should print out the full contents of the array after the code has read the file and saved its contents in the array so you can see if the code is working correctly.
Quote: | im not reading in another other rows, only the first line. |
Why not read all the lines in the file? See my pseudo code in post #30 for the steps that the code should take.
|

November 26th, 2012, 08:51 AM
|
|
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 You should print out the full contents of the array after the code has read the file and saved its contents in the array so you can see if the code is working correctly.
Why not read all the lines in the file? See my pseudo code in post #30 for the steps that the code should take. |
Alright, so i figured out how to loop through each value..
1) I loop through each row then add it to a value..
(So the first row (########) gets put in a object)
2)I run a for loop, which splits each value up...
(So since the first row have 10 values, then it will be ran 10 times. Using substring(i,i+1) to split up each value.)
3) I convert it into a char primitive then store it in the array. It find the * now.
However, i can't set the x,y properly. No matter what happens, the x value will always be 0, and once the * has been found , then it will simply make the y value 1. (0 for false, 1 for true).
Code:
char[][] testing = new char[rows][cols];
char d = 0;
int v = 0;
String value;
//Error down here.
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
if(test = in.hasNext())
{
value = in.next().toUpperCase();
for(int i = 0; i < value.length();i++)
{
d = value.substring(i, i + 1).charAt(0);
testing[x][y] = d;
System.out.print(testing[x][y]);
if(d == '*')
{
v = y;
}
}
System.out.println(" ");
System.out.println(v);
} } }
Output..
Code:
##########
0
#OOOO*#O##
1
#O####OOO#
1
#O#O#OOOO#
1
#OOOOO##OO
1
##########
1
|

November 26th, 2012, 09:01 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
What is printed out for the whole of the testing array using the deepToString() method after all of the file has been read?
Can you post the contents of the input file and what is printed out for the full contents after it is read and loaded into the array?
What you have posted does not show the complete contents of the array.
What is the code in post #35 supposed to do?
Code:
d = value.substring(i, i + 1).charAt(0);
Why both the substring AND the charAt? There should only be one needed.
Can you make a small program that reads the file and stores it in the array for testing. The posted code can not be compiled and executed for testing.
Last edited by NormR : November 26th, 2012 at 09:08 AM.
|

November 26th, 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 What is printed out for the whole of the testing array using the deepToString() method after all of the file has been read?
Can you post the contents of the input file and what is printed out for the full contents after it is read and loaded into the array?
What you have posted does not show the complete contents of the array.
What is the code in post #35 supposed to do?
Code:
d = value.substring(i, i + 1).charAt(0);
Why both the substring AND the charAt? There should only be one needed.
Can you make a small program that reads the file and stores it in the array for testing. The posted code can not be compiled and executed for testing. |
Well im trying to just store in each value of the file, rather than the whole row. In my old cold, the array would just store the whole row rather than each value in the file.
I have to find the x,y of where the file first see '*', then i have to write a method, that will check the neighbor of the * to see if there is an open space, which then i would simply keep checking the neighbor space until i finish the maze.
DeeptoString Output...
Code:
[[#,null,null,etc...]] //and it wont let me copy and paste, however, its clearly wrong. There are too many null spaces.
Input File
Code:
6 10
##########
#OOOO*#O##
#O####OOO#
#O#O#OOOO#
#OOOOO##OO
##########
Program Output after it reads to the array
Code:
##########
#OOOO*#O##
#O####OOO#
#O#O#OOOO#
#OOOOO##OO
##########
The substring and char...
Well my array have to be a char array, therefore i input the file into a string primitive, then i convert it over into a char. I use substring, so i can get each value rather than a whole line.
Program Code...
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;
boolean test;
char[][] testing = new char[rows][cols];
char d = 0;
int v = 0;
String value;
//Error down here.
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
if(test = in.hasNext())
{
value = in.next().toUpperCase();
for(int i = 0; i < value.length();i++)
{
d = value.substring(i, i + 1).charAt(0);
testing[x][y] = d;
System.out.print(testing[x][y] + " (" + (y + 1) + "," + (x + 1) + ") ");
if(d == '*')
{
v = y;
}
}
System.out.println(" ");
//System.out.println(v);
} } }
in.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Can not find file!");
}
}
}
Output..
Code:
Enter the file that you want to use(DO NOT INCLUDE THE EXTENSION:
test
# (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1) # (1,1)
# (2,1) O (2,1) O (2,1) O (2,1) O (2,1) * (2,1) # (2,1) O (2,1) # (2,1) # (2,1)
# (3,1) O (3,1) # (3,1) # (3,1) # (3,1) # (3,1) O (3,1) O (3,1) O (3,1) # (3,1)
# (4,1) O (4,1) # (4,1) O (4,1) # (4,1) O (4,1) O (4,1) O (4,1) O (4,1) # (4,1)
# (5,1) O (5,1) O (5,1) O (5,1) O (5,1) O (5,1) # (5,1) # (5,1) O (5,1) O (5,1)
# (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1) # (6,1)
As if you notice, the y value should be the x value, also, the x value isn't working properly. It should be (1,1),(1,2)etc...
|

November 26th, 2012, 06:05 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Where does your posted code call the Arrays class's deepToString() method to show the contents of the two dim array?
Why does the code do both substring and charAt() ??? Only one is needed.
I don't understand why the code has 3 loops. There only needs to be two:
The outer loop reads one line from the file and sets the index for the row in the 2D array
The inner loop goes through the characters in that line one by one and assigns them to the columns in the current row.
Last edited by NormR : November 26th, 2012 at 06:27 PM.
|

November 26th, 2012, 06:49 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 Where does your posted code call the Arrays class's deepToString() method to show the contents of the two dim array?
Why does the code do both substring and charAt() ??? Only one is needed.
I don't understand why the code has 3 loops. There only needs to be two:
The outer loop reads one line from the file and sets the index for the row in the 2D array
The inner loop goes through the characters in that line one by one and assigns them to the columns in the current row. |
I decided to just a smaller program, that inputs any value... which works fine!
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();
char row;
int a = 0, b = 0;
boolean test;
char[][] testing = new char[rows][cols];
String value;
//Error down here.
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
a = x + 1;
b = y + 1;
System.out.print("Enter the value in row " + a + " col " + b +": ");
testing[x][y] = ask.next().charAt(0);
System.out.println(a + " " + b);
} //End for y
} //End for x
in.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Can not find file!");
}
}
}
So, how can i do this correctly, with using a inputted file? Like how can i make the program input the values in each role, then go down and do it again?
(So instead of my manually inputting the code, like the program above, it does it by it self.)
And...
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;
boolean test;
char[][] testing = new char[rows][cols];
char d = 0;
int v = 0;
String value;
//Error down here.
for(int x = 0; x < testing.length ; x++ )
{
for(int y = 0; y < testing[x].length ; y++)
{
if(test = in.hasNext())
{
value = in.next().toUpperCase();
for(int i = 0; i < value.length();i++)
{
d = value.substring(i, i + 1).charAt(0);
testing[x][y] = d;
System.out.print(Arrays.deepToString(testing));
if(d == '*')
{
v = y;
}
}
System.out.println(" ");
//System.out.println(v);
} } }
in.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Can not find file!");
}
}
}
|

November 26th, 2012, 06:53 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
You need to read the line into the value variable in the outer loop with the for(x)
and extract the characters and save them one by one in the inner loop with for(y)
Get rid of the third loop with for(i)
Get rid of the use of substring(), only charAt() is needed
Last edited by NormR : November 26th, 2012 at 06:56 PM.
|

November 26th, 2012, 07:21 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 You need to read the line into the value variable in the outer loop with the for(x)
and extract the characters and save them one by one in the inner loop with for(y)
Get rid of the third loop with for(i)
Get rid of the use of substring(), only charAt() is needed |
Thank you! I finally got the code working.
|

November 26th, 2012, 08:29 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 29
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
|
|
|
Problem Solved
|
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
|
|
|
|
|