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 2 of 2

Thread: Reading String Input?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading String Input?

    So the other day I thought I might go ahead and try to make a few simple programs to see if anything I have done is soaking in. I did a few math ones, which were easy enough, but then I tried to do a password program. It was easy enough to write, but the thing is I could only do it with numbers. Although thats alright, I was aiming to do one with Strings rather than ints. Thing is I have no idea how to take the input of a String like an int would (readInt). Heres my short password program involving just ints if it helps any:

    package OtherStuff;
     
    import acm.program.ConsoleProgram;
    import java.util.*;
     
    public class stringPassword extends ConsoleProgram {
     
    public void run() {
    	int password = 2256;
    		while (true) {
    			int input = readInt("Enter a password: ");
    			if (input == password) {
    				println("Password accepted");
    				break;
    				}
    			if (input != password) {
    				println("Password not accepted\n");
    				}
    		}	
    	}
    }

    So all in all, how can I take the input of a String and use it in the same way I use ints in the code above?


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

    Default Re: Reading String Input?

    To turn a string into an integer:
    Integer.parseInt(yourString);

    All other primitive data types have similar methods.

Similar Threads

  1. Replies: 1
    Last Post: January 15th, 2010, 12:32 AM
  2. reading JMF input streams?
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 11th, 2010, 11:22 PM
  3. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 09:54 AM
  4. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 02:46 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM