1

I am confused as how to separate a sequence of numbers in a string, which are separated by spaces and finding the minimum among them (e.g. "4 3 2 1".)I have an idea of separating the values using a scanner and parsing them, but I don't know how to use them when it's a string. Please help! Thanks!

John Smith
  • 21
  • 2
  • split the input string using space into array and then find minimum on them – Gaur93 Jan 30 '18 at 06:05
  • 2
    Possible duplicate of [How to split a String by space](https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space) – Shubhendu Pramanik Jan 30 '18 at 06:09
  • @ShubhenduPramanik how exactly does that post address the _"and finding the minimum among them"_ requirement? – Phil Jan 30 '18 at 06:11
  • @Phil the sentence `I have an idea of separating the values using a scanner and parsing them, but I don't know how to use them when it's a string`. I guess OP asked how to split mainly. – Shubhendu Pramanik Jan 30 '18 at 06:13
  • To me that sounds more like _"how do I convert the strings to numbers for comparison"_ but yeah, not enough information – Phil Jan 30 '18 at 06:17

4 Answers4

2
  1. Split the string on space (ie String#split())
  2. Pass the array to a stream
  3. Use Stream#mapToInt() with Integer#parseInt and then IntStream#min() to find the minimum value

For example

final String s = "4 3 2 1";
final int min = Arrays.stream(s.split(" "))
    .mapToInt(Integer::parseInt)
    .min()
    .getAsInt();
Phil
  • 157,677
  • 23
  • 242
  • 245
1
//hope this helps

public static void main(String[] args){
    String s = "9 3 4 5 7 3 8 9 3 1";
   //split on space
    String[] arr = s.split(" ");

    int result = Integer.parseInt(arr[0]), temp;
    for(int i = 1; i<arr.length; i++) {
                temp = Integer.parseInt(arr[i]);
    //store lowest in result
        if(result>temp)
            if(temp<result) {
                result = temp;
            }
    }

//print result
System.out.println(result);

}
DhaRmvEEr siNgh
  • 1,918
  • 2
  • 13
  • 17
  • I have'nt @Phil .... I have downvoted only one and I'm going to comment the reason with that :) and I also know your answer is best on if using java 8.. so Im going to upvote that.. now cheers.. – DhaRmvEEr siNgh Jan 30 '18 at 06:21
  • Fair enough. Just hate seeing a rash of downvotes with no comments – Phil Jan 30 '18 at 06:26
0

For Splitting the String you can use (For Example):

String str = "4 3 2 1";
String[] splited = str.split("\\s+");

as for the individual number in the array "splited" you can use

int Num = Integer.parseInt(splited[INDEX]);

or you can directly use the Integer.parseInt(Splited[INDEX]) in the condition of IF loop.

int min=Integer.parseInt(splited[0]);
for(int i=1;i<splited.length;i++){
  if(min > Integer.parseInt(splited[i])){
     min = Integer.parseInt(splited[i]);
  }
}
  • 1
    why are you converting 'splited' string twice ? once when comparing once when when assigning to min... – DhaRmvEEr siNgh Jan 30 '18 at 06:22
  • Can you specify where I am converting twice? And are you the one who down voted mine? – Ghostranger Jan 30 '18 at 06:31
  • when you are comparing your current value to min(if condition) you are converting that and if that gives true, again you are converting the same to assign that to min... the better way will be convert it before if block and store it into a variable, compare that variable with min and if true assign the same to min. – DhaRmvEEr siNgh Jan 30 '18 at 06:35
  • It doesn't Matter, there is no change in the time complexity. What are you trying to prove just specify. With your approach you are using an extra Int variable which increases the space complexity. – Ghostranger Jan 30 '18 at 06:39
  • 1
    are you trying to say that using Integer.parseInt(splited[i]) twice for same String will take same time as using Integer.parseInt(splited[i] once .. why? can you please explain? – DhaRmvEEr siNgh Jan 30 '18 at 06:46
  • You can try it out your self. By calculating time complexity manually or you can use `import java.util.Timer;` for computation of huge length of the array. and for your kind information I have specified `int Num = Integer.parseInt(splited[INDEX]);` or my loop as an answer. – Ghostranger Jan 30 '18 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164148/discussion-between-dharmveer-singh-and-ghostranger). – DhaRmvEEr siNgh Jan 30 '18 at 07:07
-1

There are parsers for each datatype. In your case you need to parse a string into an int, so do

Integer.parseInt(str);

Here is the javadoc.

Vj-
  • 722
  • 6
  • 18