Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-20-2011, 02:47 AM   PM User | #1
Gomez
New to the CF scene

 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Gomez is an unknown quantity at this point
String to INT

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.

Code:
	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 );
}
}
Gomez is offline   Reply With Quote
Old 03-20-2011, 02:54 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by Gomez View Post
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.

Code:
	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 );
}
}
java.util.regex, Pattern and Matcher?

best regards
oesxyl is offline   Reply With Quote
Old 03-20-2011, 02:44 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
pattern matching will help you to tell if it is a number, but still won't cast it so you'll still need the parse if you want to treat it as a number.
Just try/catch it instead, its much simpler. Integer.parseint will throw a NumberFormatException if it can't be parsed:
PHP Code:
try
{
   
Integer x Integer.valueOf(S1);
   
System.out.println("The int is: " x);
}
catch (
NumberFormatException ex)
{
    
System.out.println(S1 " is not a number.");

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:42 PM.


Advertisement
Log in to turn off these ads.