• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with the scanner class.

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the Stacktrace please? And the flow as I think would be:
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try putting the code which reads the line from the file outside the for loop.
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that does not work even.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, i could solve this issue. I got rid of the for loop and made some other changes.

-ADi
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done. Please tell us how you solved the problem.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic