Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: reading string input then casting it to an int?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Cool reading string input then casting it to an int?

    hi,

    if someone inputs a number in a word (i.e. "two"), how can i take that input and convert it to an int?

    -etidd


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: reading string input then casting it to an int?

    see this post:
    Words and numbers

    edit:

    that link is for converting an integer value to a String. Cuju has the right idea, though

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: reading string input then casting it to an int?

    Quote Originally Posted by helloworld922 View Post
    see this post:
    Words and numbers

    edit:

    that link is for converting an integer value to a String. Cuju has the right idea, though
    I deleted it by accident lol, here it is again:

    import java.io.*;
    import java.util.*;
     
     
    public class ConvertString {
     
    public static void main(String args[]) {
     
    String input = "";
    int number = 0;
    Scanner key = new Scanner(System.in);
     
    System.out.println("Enter a number");
    input = key.next();
     
    //CompareTo function
    if(input.compareToIgnoreCase("two")==0) {
    	number = 2;
    	System.out.println(number);
    }
     
    //equals function
    else if(input.equals("three")) {
    	number = 3;
    	System.out.println(number);
    }
     
     
     
    }
    }

Similar Threads

  1. Reading String Input?
    By Morevan in forum Java Theory & Questions
    Replies: 1
    Last Post: January 18th, 2010, 12:16 PM
  2. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  3. reading JMF input streams?
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 12th, 2010, 12:22 AM
  4. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM