• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList collection maker  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code scans TextFile. Every word  is converted into collection element.
How to filter int values?

Add only elements to collection, who contains letters only, no numbers.




Text file:

Potato  45551  54545 liquit
456554  Brokol     788
Tomato 456
console.png
[Thumbnail for console.png]
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest making a method to do the character verification. Something like

Loop through each letter in 's' and if any one of them is not a letter return false.
Make use of the String.charAt() and Character.isLetter() methods.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go and have a look at the Documentation for Scanner. If you use it without any tweaking, Scanner starts with the first non‑whitespace character and keeps going until it finds whitespace again. That is not one word but one token. It recognises tokens separated by whitespace, remembering that line end characters count as whitespace too.
I shall suggest the alternative approach: use the methods of Scanner to test whether the next token is an int. That will only work for your current file; if your file contains 123 234 and 456 all is well, but it will not recognise a number with fractions or a decimal point by that technique.
if (myScanner.hasNextInt()) { ... } else { ... }
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point about using scanner. You probably won't be able to use nextInt() though because it wouldn't handle "abc123" correctly.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go and have a look at the Documentation for Scanner.


i dont understand where to put correctly these code portions. Even if compilator show no mistakes, I get no results.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you implement Carey's first post, the isOnlyLetters() method.  It is probably more straightforward and easier to code.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I would suggest you implement Carey's first post, the isOnlyLetters() method.  It is probably more straightforward and easier to code.


I have found this .




Im even not sure if it fits my scenario. Also compilator marks several errors.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you find that code? It looks to me as though it will work all right, but if you hand it in for marking, you will be recognised as not having written the code yourself and you can't get any marks for that.
What compiler errors are there? The code looks to me like something that will compile OK.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I write that allLetters method with a Stream?You take the word parameter and create an IntStream with its chars() method (or more precisely CharSequence#chars()). That IntStream object does nothing until the final method runs; because it doesn't create another Stream, it is called a terminal operation. The allMatch() method goes through the elements of the Stream expecting to return true; it terminates as soon as it finds something which doesn't fulfil its requirements. Passing the term Character::isLetter tells the JVM to look for that method and apply it to each int in the Stream; the isLetter() method is overloaded and you will use the version taking an int parameter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:I have found this .

Seems fine, but why is it called target()?

Also, just FYI, you don't need to define a variable, viz:
However, Campbell's solution is much more elegant IMO.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can I write that allLetters method with a Stream?

Hmmm. Am I right in thinking that something like this might work then?
Winston
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston

Thanks for this.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . .
Winston
I think that will work, but I am a bit suspicious about methods starting forEach. I think you need a separate method to tell whether the word is all letters, and I would prefer the methods of the Streams that create a new List on the fly.That is (as far as I can tell) the same as I posted earlier, and I have already explained it. What I hinted at, but didn't expand on, is that the allMatch method has a preference for returning true. I think that means it will return true vacuously if passed an empty Stream. The anyMatch method goes the other way, with a preference for false. At least I think it does.
Now, let's see if we can't use a method reference to call that method. You can either use object name to the left of :: or a class name if it is static. Let's try the latter; object names shouldn't be used for static members anyway. I do not know whether you can miss out whatever is left of the :: but you probably can't. I am going to assume that all the words have already been added to a List.All Lists have a stream() method (actually inherited from Collection) which returns a Stream of the same type as the List, i.e. a Stream<String>. You can use its filter() method to create a second Stream of the same type, but elements matching the predicate only, i.e. anything which causes isAllLetters to return true. Now we have a second Stream<String>, we can use a method to collect all its elements into a List<String>. You want the collect() method, which takes a Collector as its parameter. Now, Collector is a bit difficult to understand, but when you scroll down, you find a link to the Collectors class and when you look in that link, there is an example of creating a List<String>. Armed with that knowledge, we look for the Collectors#toList() method and find it returns a Collector that creates a List. So Bob's Your Uncle
Note that once you have got the List, you don't need to import Stream, but you do need to import java.util.stream.Collectors.
I think the runtime type of the List returned is our old favourite ArrayList.

And thank you for the nice things you said about my earlier post
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for efforts.
But I have recently learned loop, switch-case, printwriter/scanner. And still about basic uses. And this is what I know in total.

I simply cannot comprehend these StreamBuffers, code liquids. Same applies for javadocs, javapoint, tutorialpoints...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you need a separate method to tell whether the word is all letters, and I would prefer the methods of the Streams...

Yes, I actually came up with something very similar as your solution with filter() when I was thinking about it. However, it requires two complete passes through the "stream", whereas mine (if it's valid) only requires one.

Pity that Scanner doesn't have a stream() method.

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
But I have recently learned loop, switch-case, printwriter/scanner. And still about basic uses. And this is what I know in total.


Well, hopefully, you are not turning this code in as part of a homework assignment. As this will be ridiculously easy to determine that you didn't do the assignment yourself. This is definitely not "basic stuff".

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:Thanks for efforts.
But I have recently learned loop, switch-case, printwriter/scanner. And still about basic uses. And this is what I know in total.

My apologies. I kind of "usurped" your thread to ask a question that was bothering me.

bob john wrote:I simply cannot comprehend these StreamBuffers, code liquids. Same applies for javadocs, javapoint, tutorialpoints...

My advice: Take one thing at a time. And my advice would be to start with javadoc, because it's an incredibly useful tool.

Unfortunately, the state of much of the version 8 (especiall Stream and "function") docs that have been written is very poor. because the syntax (even to me) is not obvious without examples.

Winston
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I have decided, that would be vital to have a code example.
Who all file values converts to ArrayList collection, but this time Only numbers, no String values.

I cant find out by what word 'chars' should be replaced at 13th line.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are several problems, but start with this:

word needs to be a String, then chars() will work.  Also, if you already know that word is a Integer, why test it?  So it needs to be String.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!