• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting String to Int Array

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rancher,

I am back needing your expert guidance! Working on my newest assignment with Arrays. I have read all my modules/book chapter and watch a bunch of tutorial videos and I thought I was some what understanding arrays until this assignment Even my tutor wasn't able to guide me with this one she wasn't quite understanding the logic, any who! I am here to ask for your help again. Below is the code for all the methods my assignment instructed me to do. In the end I have to enter an input "s 1 2 3 o 4 5 6" that should output as [1,2,3,4,5,6]. I know right now I have a few methods that aren't being "called" anywhere which is my next struggle lol but I just want to know if the logic in my current methods make sense. I have tried so many different things and can't seem to wrap my head around this one

import java.util.Arrays;
import java.util.Scanner;


 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Split the String into a String[].
Iterate the array.
Test whether each String represents an int, which you can do by passing that String to a Scanner.
If it is an int, keep it in an int[] array, otherwise ignore it.
Count how many ints you have actually found; use one of the copyXXX() methods of the Arrays class to trim the resultant array to the size you actually want.

Much easier done with Streams.Show your teacher that code working nicely, and don't tell her where you got it from
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
thank you for taking the time to explain what I need to do. Unfortunately I don't quite understand. Maybe I have spent too much time of this and have done too many different things and nothing is making sense anymore I will keep trying based on what you mentioned and see if my brain can turn it's light on and understand! Some of the code you put is new to me so I need to try and understand what that is doing. thank you for the information I will try and understand that and try to make it work!
To believe I studied Java 20 years ago and was actually good at it then... really wish I would have kept up with it!
thanks again!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Josee Landry wrote:. . . thank you . . .

That's a pleasure If you did it once, you can do it again.

I suggest you break down what I suggested into small stages. Do one part; maybe splitting the String into a String[], and post what you have achieved. Then move on to the next stage.

Campbell's JShell wrote:jshell> String input = "s 1 2 3 o 4 5 6";
input ==> "s 1 2 3 o 4 5 6"

jshell> String[] tokens = ???;
tokens ==> String[8] { "s", "1", "2", "3", "o", "4", "5", "6" }


 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! that's great advice! you are right I need to break it down 1 piece at a time. I will get back at it later after a mental break! thanks again
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: (...) Show your teacher that code working nicely, and don't tell her where you got it from


Hmm, better wait a little, since we also have to deal with the boolean skipErrors.

I just produced an elegant but pretty complex variant, that uses "Pattern.compile("[1-9][0-9]*").asPredicate" and a partitioningBy, but I can't show it here, since if Cambell notices, he will remove it    
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, he won't because he knows it will confuse OP's teacher even more
If you are using Pattern#compile, what will happen to a String valued "0"? Or if you pass "2147483648" without a −?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I thought that 'Integer.parseInt("06")' would give an error, therefore I was desparately looking for a pattern that accepts either a single '0' or a number that did not start with a zero. I'm no hero when it comes to regexes, and so I failed miserably. I then produced a compound Predicate:

But since 'Integer.parseInt("06")' turned out to be perfectly legal, this could be greatly simpified.

And indeed, I did not take integer overflows into consideration. I have changed Integer::parseInt to Long::parseLong and replaced Integer::MIN_VALUE by its Long counterpart.

Anyway, maybe having a separate Scanner for each token IS simpler. But to prevent that, I thought of using a Predicate.
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Campbell its not my teacher that will be confused its me! haha

this was a difficult assignment for this newbie but I had to submit something. It wasn't great but I have to move on to the next! I realise Arrays are challenging at the moment so I will go through my chapters again and hopefully things will sink in!

have a wonderful day all!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us what you handed in. i would do this sort of thing, with the reservation that a Streams solution (as before) is more elegant. We usually avoid giving out complete code, but it can't do any harm now you have handed your assignment in:-You can only use var to represent a type in Java10+ and only for local variables; you could use int/String/Scanner instead. It is awkward (if not impossible) to use var for an array.
The split() method takes a regular expression: "\\s+" reduces to \s+ which means ≥ 1 whitespace character. You would probably get " " (← don't copy'n'paste) to work, too.
You can telescope lines 10‑11 by moving the ++ operator.

[addition]There are several other ways you can solve this exercise. To learn Streams, I suggest you get a copy of Java8 in Action or Modern Java in Action by Urma Fusco and Mycroft (Manning). They aren't different boks by different names for different editions of the same book.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must keep track of whether there are any non-numbers present in the tokens. This, and the value of the boolean 'skipErrors', will determine the actual output. See the method 'scanStringToIntArray' in OPs openng post.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Much easier done with Streams.



Nice. Alternatively,
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . 'skipErrors' . . . See the method 'scanStringToIntArray' in OPs openng post.

Oh, is that what skipErrors means. Was it in the original specification?

Junilu Lacar wrote:Nice

Your solution is nice, too
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell, to be honest I submitted what I had I never got to figure out how to make it work and I had to submit it due to deadline. I have to follow the instructions I get and there was thing you mentioned that I hadn't learned yet and I still need to follow all the methods we are asked to complete. it happen I got C- on this assignment but A+ on all the others. I received sample code for the assignment afterwards that I need to figure out and understand and then move on! I am finding JAVA quite challenging and hard to learn via online studies but I will get there!

P.s. I appreciate that you don't give out coding. I only post here to get a nudge or just have someone say ok this method or this code line XX is incorrect and then I have somewhere to start, this is how I will learn!

thanks to All for your inputs I appreciate your time!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Josee Landry wrote:I received sample code for the assignment afterwards that I need to figure out and understand and then move on!


I'm sure people here would be interested in seeing that code as well, if it's something you're allowed to share. We can help you grok it and comment on its pros and cons.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with my suggestion from yesterday with Streams.Line 1 should be obvious
Read this blog by Cay Horstmann, who is an occasiona visitor to this website. He complains (quite rightly) that Scanner was forgotten when Streams were introduced; fortunately two methods for getting a Stream from a Scanner were introdued in Java9 (as well as two methods to give more control over starting and stopping a Stream). So we can get a Stream<String> out of that Scanner, which processes the eight tokens in the original String.
A Stream doesn't “have” elements ,but processes them. There are three methods called intermediate operations because they produce different Streams, and when we get to the terminal operation (toArray()), they will actually start. That is called lazy execution. A Stream needs a source of information, and the Scanner here acts as that source.
Anyway, line 2 creates the Scanner, and line 2 creates the first Stream, handling s-1-2-3-o-4-5-6: eight tokens.
You want to get rid of anything not an int, and Scanner has a nice method for doing so. The syntax with -> is doubtless unfamiliar to you; have a look in the Java™ Tutorials to learn about it and how you are here creating an instance of the Predicate interface. The filter() method creates a new Stream of the same sort as previously, but minus all elements for which the Predicate method returns false. As an alternative, Junilu created a Stream<Scanner> where the Scanners “carry” the text with them.
The mapToInt() call in line 5 creates a slightly different type of Stream. It does it by using the well‑known Integer#parseInt() method directly, and in line 6 we simply use this method, whose name makes its function obvious.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code that I had, as I interpreted the assignment
 
Josee Landry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Junilu unfortunately I can't post it here as other student in the next semester could find it. I know they slightly change the assignment but that would give it away.

@Campbell thanks for all that information. I will review the links you provided and start learning about stream and the -> as you are correct never seen that yet! I have only a couple weeks and 1 last big assignment until this course is done. I will keep going through everything Ive learned and look at these new things more before my next session.
no worries I am sure you will be seeing another post from me soon needing help with my final assignment lol

@Piet thank you for that as all. I am taking all this in and will try it and keep learning from it. for now I have to focus on my next assignment but I have a lot of good information to review here later and keep learning!

again thanks everyone!!! talk soon ;)
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure

I think it is unfortunate that you have got through a whole module without encountering at least the simpler uses of the Streams API.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic