This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Issue with the scanner class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Issue with the scanner class." Watch "Issue with the scanner class." New topic
Author

Issue with the scanner class.

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hello Folks,

Wish you all a merry christmas. I have some issues while scanning a text file and replacing some strings in it. The string that needs to be changed have been stored in an array. I am getting java.util.NoSuchElementException error. Can you let me know what am i doing wrong here? Hints will do too.

Thanks
Adi





The Text File Look like this:-



The Desired output should be:-

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Can you post the Stacktrace please? And the flow as I think would be:


Mohamed Sanaulla | My Blog
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hello Mohamed,

Thanks for the reply. I was following the same pseudocode as you gave me. I was seeing the issue in the "for" loop inside the while loop, i.e. the first element in the array was not being searched. The above array example i gave was wrong, the array in my case would be sorted in the order it occurs in the text file, i.e String[] test = {"CONFIGURE_KDS", "CONFIGURE_KC5"};

I made some changes in the method like below. Added a primitive style increment for variable "i". Can anyone let me know why the "for" loop does not work. It only look for the second element in the array and not the first one? The following method works, how can i make it more efficient?

-Adi


Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You might try putting the code which reads the line from the file outside the for loop.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

that does not work even.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Aditya Sirohi wrote:that does not work even.

Can you provide us the working code. And also the output you are getting for that code?
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hello Mohamed,

Here you go. I really appreciate the help. I need to have a for loop there because later i need to get the value of the content in the array from hashmap, So the content in the array acts as a key.

-Adi



The content of test.txt is:-

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Why have you commented the for loop there? In Line 31

Instead of line 35-line 42, you could try this:


And incrementing the value of the index- i in the for loop would happen irrespective of whether the String from the array was found in the line read from the file. Also you can actually break out of the for-loop if you think that comparing other elements in the String with the line is not required.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Why are you using a Map and using the index to retrieve the String from the Map? That sounds as though a List might be easier; you can count indices in a List.
Get rid of the @SuppressWarnings("unusued") and get rid of the unused local variables.
You appear to be reading a nextLine and a next in the same iteration of the loop; that looks very peculiar to me.
Why can't you simply use the String#replaceAll() or String#replaceFirst() methods?
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks everyone, i could solve this issue. I got rid of the for loop and made some other changes.

-ADi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Well done. Please tell us how you solved the problem.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

And please post the code which solved your problem. And not commenting out different lines which would make it more confusing to understand how it actually works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with the scanner class.