I am trying to return a string of names in a list as an integer, the names are pulled from a longer list. i have managed to get the list of names needed but cannot figure out how to convert the string into an integer. see code below:
public static void doStuff(List<Seat> uk){
for(Seat s:uk)
if (s.place.contains("Edinburgh"))
System.out.println(s.results);
This is where i get stuck does anyone have any advise on how to solve this?
You can use Integer.parseInt("123") to convert a String ("123") to an Integer (123).
What String are you trying to convert though ?
Let me know if it works (or not)
Happy coding :) -Charlie
To convert a string to an int:
String s = "5";
int i = Integer.parseInt(s);
Your question is a bit confusing, so if this isn't what you're looking for, let me know and I'll update my answer accordingly.
Related
I'm practicing Java online and one of the practice problems is to:
"Write a method called largerAbsVal that takes two integers as parameters and returns the larger of the two absolute values. A call of largerAbsVal(11, 2) would return 11, and a call of largerAbsVal(4, -5) would return 5."
I have already wrote my method solution which so far is:
public static int largerAbsVal(int a, int b) {
return Math.max(Math.abs(a), Math.abs(b));
}
But for some reason, the website keeps telling me that my code caused an error of type NumberFormatException? I already declared the two parameters as integers at the heading so what exactly is wrong with my code in this case?
Any help is greatly appreciated!
From NumberFormatExecpton:
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
The code you posted works great! My guess is your error is somewhere before that piece of code where you're converting a string to an int. Example:
Integer.parseInt(string)
And that the string given is malformed (i.e. not an int). For example, something like "2.1" will throw an error like this.
Hope this helps!
I am new to Java so forgive me if this is a silly question.
First I did the following (this is a section from my code):
public static void team()
{
int score = JOptionPane.showInputDialog("Enter the team score...");
calculate(score);
}
But I kept getting an error saying: "Incompatible types string cannot be converted to int".
So I thought I may need to use parsing. I did the following and it worked:
public static void team()
{
int myScore;
String score = JOptionPane.showInputDialog("Enter the team score...");
myScore = Integer.parseInt(score);
calculate(myScore);
}
I would like to know why there is a need for me to parse the variable "score"? In the first piece of code, I declared the variable score as an integer, so would it not store the input which I expect to be an Integer in the variable score which is then passed into the method calculate. I understand that parsing is when you convert a String into an Integer. However in this case, I do not expect the user to input a string so why is there a need for parsing?
The simple reason is: because JOptionPane.showInputDialog() returns a String, regardless of what you actually want the user to input.
There's no mechanism in Java to do automatic conversion based on the type of your destination variable myScore (though some other languages can do this, e.g. implicit conversions in Scala).
You could (in principle) create a custom dialog that returns an int , e.g. by getting the user to choose from a pulldown list of numbers, and then no parsing would be needed. But otherwise, you have to do parsing explicitly.
The JOptionPane.showInputDialog("Enter the team score..."); returns a String which you tried to store in a int. That is why you are getting the "incompatible types".
See Javadocs for more information.
Because a string is stored differently than an integer. Integers are stored in binary, while as a String is a collection of characters.
Example:
The number 14 would be stored as 1110 in Binary. However, as a String, it's stored as 00110001 00110100
Take a look at this http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp
Integer.parseInt() basically does the dirty work for you, by looking up the characters in a lookup table, and as long as they correlate with proper integer values, it coverts it over to binary form, to be stored in an integer.
I have a String: a%sb%sc%s. I need to format b before I format a or c, but I'm not sure how or even if I can specify only to format b while keeping the rest of the String unformatted.
In other words, I'm trying to do this:
String.format(foo, "test");
With the outcome:
a%sbtestc%s
Is it possible to manipulate a String like this or should I just use String.replace instead?
A little more detail. The ultimate String will look something like: aA-PARMbB-PARMcC-PARM and then used to fetch some data. a and c are much more dynamic than b, so I'm trying to format b before hand.
So, again. I'm trying to achieve the following:
String.format(foo, "B-PARM");
With the results:
a%sbB-PARMc%s
Then format the rest:
String.format(formattedFoo, "A-PARM", "C-PARM");
You could do your formatting in steps,
String aString = String.format("something %s something else", "a string");
String bString = String.format("...%s...", "test");
String cString = // ....
String completeString = String.format("a%sb%sc%s", aString, bString, cString);
but again, I have to wonder what is going on, and whether this represents an XY Problem, one that is best solved by a completely different approach. Consider giving us the details of the overall problem that you're trying to solve and perhaps not the code tactics that you're using to try to solve it.
I am doing a method that can convert a int number to hexadecimal string. Basically my code is work for all test, but I am still looking for an efficient way to get rid of the array part. Anyone would give me a hand?
the better way for not using array, but rather use a string type would be more efficient, and the answer is very clear as above. So i just delete my original code
Integer.toHexString(int);
Should be what you are looking for
String digits = "0123456789ABCDEF";
output = digits.charAt(remain) + output;
I hope there are no convertion problems.
I Think the Integer.toHexString(int) is the best way to fix your problem
I am working on a BB application in which I am getting a long String from server.
Now I want to convert that string in to String Array of words present in the String, so that i can compare the complete Array elements one by one with my hard coded fields and can display them for user.
Please let me know the with your useful suggestion.
The split() is not available for Java 1.3 (CLDC 1.1).
Did you already look at net.rim.device.api.util.StringUtilities#stringToWords?