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:
  #16  
Old November 25th, 2012, 04:37 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
Quote:
help me with my error

Please post the full text of the error message. Each post has an id number in the upper right. Where is the error message?

Reply With Quote
  #17  
Old November 25th, 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
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)

Reply With Quote
  #18  
Old November 25th, 2012, 05:19 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #19  
Old November 25th, 2012, 05:35 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
Ok, fixed.

Now what about the issue when finding the starting point? It should say (2,6) however, nothing comes up.

Reply With Quote
  #20  
Old November 25th, 2012, 05:52 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #21  
Old November 25th, 2012, 06: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
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 (*) ...

Reply With Quote
  #22  
Old November 25th, 2012, 06:30 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
[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 #

Reply With Quote
  #23  
Old November 25th, 2012, 06:44 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=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.

Reply With Quote
  #24  
Old November 25th, 2012, 06:47 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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

Reply With Quote
  #25  
Old November 25th, 2012, 06:51 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
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);

Reply With Quote
  #26  
Old November 25th, 2012, 06:57 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #27  
Old November 25th, 2012, 07:07 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
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]]

Reply With Quote
  #28  
Old November 25th, 2012, 07:15 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #29  
Old November 25th, 2012, 07:51 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
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.

Reply With Quote
  #30  
Old November 25th, 2012, 08:02 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 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 29 m 42 sec
Reputation Power: 345
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.

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