Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #31  
Old November 25th, 2012, 08:12 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 *.

Reply With Quote
  #32  
Old November 25th, 2012, 08:15 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
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.

Reply With Quote
  #33  
Old November 25th, 2012, 09:31 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #34  
Old November 26th, 2012, 06:51 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
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.

Reply With Quote
  #35  
Old November 26th, 2012, 08:51 AM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #36  
Old November 26th, 2012, 09:01 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
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.

Reply With Quote
  #37  
Old November 26th, 2012, 05:06 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #38  
Old November 26th, 2012, 06:05 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
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.

Reply With Quote
  #39  
Old November 26th, 2012, 06:49 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!");
		}

	}

}

Reply With Quote
  #40  
Old November 26th, 2012, 06:53 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
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.

Reply With Quote
  #41  
Old November 26th, 2012, 07:21 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #42  
Old November 26th, 2012, 08:29 PM
cyimking cyimking is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 29 cyimking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 19 m 56 sec
Reputation Power: 0
Problem Solved

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Reading in a file Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap