Monday, May 24, 2010

String to Int conversion in JAVA?

This is what I want to do. User enters a value accepted as a String in the format: d3 or 3d.Program checks if one of the two characters is 0-9, if it finds one that is 0-9 it converts it to an int and stores it. My other question is about the letter in the entry. For example with d3, I want to associate "d" with the integer 4. "a" is 1, "b" is 2 etc. If the user enters d3 or 3d I want the program to store 3 as an int, then store 4 as another int since "d" is the 4th letter in the alphabet. How do I do this, is there a method that I can use? If you know how to do it in C, C++ or JAVA(preferred) please tell me.

String to Int conversion in JAVA?
There is probably a better way, or at least a more streamline way, of doing this, but off the top of my head here is what I would do:





1. Create a loop to run through all the characters in your string.


2. Test each character to see if it falls between ASCII values 47 - 58 (i.e. 0-9). If it does then the character is a number.


3. If the first test fails then you know that it isn't a number; therefore, you know that it must be a character, so now test to see if it is a character between 'a' and 'd' (i.e. ASCII 97-100).





Here is some sample code (hopefully it can help):


public static void main(String[] args){


String teststr = "d3";


char x;





System.out.println("Test String:" + teststr);





for(int i = 0; i %26lt; teststr.length(); i++){


x = teststr.charAt(i);


if(x %26gt;= 47 %26amp;%26amp; x %26lt;= 58){


// Store it however


System.out.println(Integer.parseInt(""... x));


}


else if(x %26gt;= 97 %26amp;%26amp; x %26lt;= 100){


if(x == 'a')


System.out.println("a=" + 1);


else if(x == 'b')


System.out.println("b=" + 2);


else if(x == 'c')


System.out.println("c=" + 3);


else if(x == 'd')


System.out.println("d=" + 4);


}


}


}
Reply:How about using java.lang.Character?


String input = "3D";


for(int i = 0; i %26lt; input.length(); i++)


{


if(Character.isLetterOrDigit(


input.charAt(i)))


{


if(Character.isLetter(


input.charAt(i)))


{


column = input.toLowerCase().charAt(i) - 'a';


}


else


{


row = Integer.parseInt(


Character.toString(input.charAt(i)));


}


}


}
Reply:There can be multiple ways to do this... the one that I'd suggest is:


[this is based on the assumption that user enters only 2 chars]





1. From the input string, split the first char and second char and store them into separate Strings (you can do this by using substring).





2. Create a method that takes a string as an input and does the following:





private int getInt(String inStr) {





int outInt;


try {


outInt = Integer.parseInt(inStr);


} catch(NumberFormatException e) {


if(inStr=="a")


outInt = 1;


else if(inStr=="b")


outInt = 2;


else if(inStr=="c")


outInt = 3;


else if(inStr=="d")


outInt = 4;


}


return outInt;


}





What this method will do is - if the inStr is actually an int represented as String, it will return the corresponding int value, but if it actually is a char (rep as String), an exception will be thrown, which will be caught in catch and outInt will be set to appropriate value using the if blocks.





Hope this helps.
Reply:To convert a string to an integer you'll want to use the parseInt method of the Integer class:





String s = "100";


int i = Integer.parseInt(s.trim());





I would need to better understand the nature of the data users are entering and why they would enter it in the way you described to be able to comment on the other part of your question.


No comments:

Post a Comment