Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to int while containing characters

Tags:

java

Simple question but I am having trouble. I have an String input that is in this format 12:43. (MM:SS).
I am trying to convert this string in to int (seconds). The only part I don't know how to do is how to get 12 and 43 without getting invalid double error. Because it is containing that ":" in the string, I can't do the usual Parse.parseInt(string); .

like image 779
Chad Bingham Avatar asked Nov 25 '13 19:11

Chad Bingham


People also ask

Does parseInt work with characters?

parseInt() method only works with string type variables. Hence, the character 'a' is converted into a String .

What happens if you atoi a string?

Description. The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

Can you convert a string to an int?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.


1 Answers

  • split the string according to :
  • For each int in the resulting array, apply parseInt
like image 56
Maroun Avatar answered Sep 22 '22 09:09

Maroun