Question:
JAVA program help needed?
Cynthia Gomez
2011-03-19 18:48:47 UTC
I am trying to convert a string to int value. Is there anyway to implement a if else statement in this so if the user inputs anything else besides an int . it would display an error. otherwise if the user inputs a string of int's it displays the int's.


public static void main(String[] args) {

System.out.println("Enter your string to be converted");
Scanner Keyboard = new Scanner(System.in);

String S1 = Keyboard.next();

Integer x = Integer.valueOf(S1);
System.out.println("The Int is:" + "\n"+ S1 );
}
}
Three answers:
anonymous
2011-03-19 20:32:31 UTC
/*This program reads in a string from the keyboard, chops the string into tokens,

* checks each character in the token to see if they are ALL digits( no 7B or 5! ).

* /



public static void main(String[] args)

{

boolean itIsAnInt= true;



Scanner keyboard = new Scanner(System.in);



System.out.println("Enter int values separated by spaces: ");//make it user friendly

String str = keyboard.nextLine( );



Scanner sc = new Scanner(str); //So we can break the string into tokens (parts)



while( sc.hasNext( ) )

{



String temp = sc.next(); //read in a token

for(int i = 0; i < temp.length( ); i++)

{

if( !Character.isDigit(temp.charAt(i) ) //If a token contains a caharcter that is not a digit

{ System.out.println( "Error: Not an int value!");

itIsAnInt= false;

break;

}





}//end of for-loop



if( itIsAnInt ) //The token had all digits so it is an int

{

System.out.println("The Int is:" + Integer.valueOf(temp);



}



}//End of while-loop





} //End of main method
?
2011-03-20 05:52:32 UTC
Not a big problem.Just think simply.

The trick is whenever you enter an integer from keyboard it's parsed properly using Integer.valueOf(String arg0),but when you are entering a string(even that contains number) an exception is thrown because that can't be parsed to an integer.So just a simple "try catch block" will deal with this.



Here you go:





import java.util.Scanner;

public class Demo

{

public static void main(String[] args) {

String S1 ="";

System.out.println("Enter your string to be converted");

Scanner Keyboard = new Scanner(System.in);

try

{

S1= Keyboard.next();

Integer x = Integer.valueOf(S1);

System.out.println("The Int is: \n"+S1);

}

catch(Exception e){

System.out.println("ERROR:You have not entered an integer.You entered this string : \n"+S1);

}

}

}





Hope this is what you want.
Edwin Torres
2011-03-20 02:19:25 UTC
Good start so far. You just need to add a loop and a condition:



Scanner Keyboard = new Scanner(System.in);

System.out.println("Enter your string to be converted");

do {

if (Keyboard.hasNextInt()) {

String S1 = Keyboard.next();

Integer x = Integer.valueOf(S1);

System.out.println("The Int is:" + "\n"+ S1 );

break;

} else {

System.out.println("invalid value");

System.out.println("Enter your string to be converted");

Keyboard.next();

}



} while (true);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...