How to convert an entire string?

Asked

Viewed 3,666 times

-5

How do I convert a value of type string that needs to be stored in a variable that is of the type int? Here are some conversion codes which are most appropriate to use in this conversion?

A.a = Integer.valueOf("10")
B.a = int.Parse("10")
C.a = Convert.Numeric("Macedo")
D.a = Convert.ToNumeric("10")
E.a = int.Convert.valueOf("Macedo")
  • Your doubt is not at all clear and the code presented does not make much sense regarding doubt. Please edit the question and be more specific about your doubt and the relationship to the code. Another tip is to always provide a [mcve] code.

  • Ezar, welcome to [pt.so]! This is not Java. Please edit your question and add more information about the language, the version you are using and what you are trying to do, after all each conversion mode can have strengths and weaknesses. Hug!

  • Would that be what’s in that question here? Convert string to entire Java

1 answer

2

Java provides the following method:

int one = Integer.parseInt("1"); // Retorna um tipo primitivo (int)

This method takes a string and converts to an integer, just as you need.

Integer one = oneInteger.valueOf("10"); // Retorna um Objecto Integer
int.parse("10"); // Não existe porque um tipo primitivo não pode ter métodos associados

As for the other examples, these methods simply do not exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.