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

Thread: Newbie question - print out strings from input

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie question - print out strings from input

    Hi all, I'm just getting started with Java and created a simple app. Please see the code and the output. Where did I go wrong? Do I need to set the variable of "display" on top of the input? Why is the input not being stored as a variable?

    import java.util.Scanner;
     
    public class UserInput {
    	public static void main (String args[]){
     
    		Scanner jason = new Scanner(System.in);
    		String fname;
    		String lname;
    		String addr1;
    		String addr2;
    		String display;
     
    		System.out.println("Enter first name: ");
    		fname = jason.nextLine();
    		System.out.println("Enter last name: ");
    		lname = jason.nextLine();
    		System.out.println("Enter Address 1: ");
    		addr1 = jason.nextLine();
    		System.out.println("Enter Address 2: ");
    		addr2 = jason.nextLine();
     
    		display = ("fname, lname, addr1, addr2");
    		System.out.println("The information you entered is: "+ display);
    		}
    }

    Enter first name:
    Joe
    Enter last name:
    Rogan
    Enter Address 1:
    123 Fastlane
    Enter Address 2:
    LA CA 90211
    The information you entered is: fname, lname, addr1, addr2


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Newbie question - print out strings from input

    display = ("fname, lname, addr1, addr2");

    since you have all the variables inside one big " " you are treating them as a string instead of variables

    you could do

    display = fname + " " + lname + " " + addr1 + " " + addr2;

    but i would imagine there is a better way but this way works lol

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Newbie question - print out strings from input

    Thanks sleepy, that worked, but I ended up with the following to make the formatting look better:

    //allow user input with Scanner utility 
    import java.util.Scanner;
     
    //create class named UserInput
    public class UserInput {
     
    	//set main
    	public static void main (String args[]){
    		//set scanner variable name to jason and allow system input		
    		Scanner jason = new Scanner(System.in);
     
    		//declare variables as strings
    		String fname;
    		String lname;
    		String addr1;
    		String addr2;
    		String display;
    		String answer;
     
    		//get user input				
    		System.out.println("Enter first name: ");
    		fname = jason.nextLine();
    		System.out.println("Enter last name: ");
    		lname = jason.nextLine();
    		System.out.println("Enter Address 1: ");
    		addr1 = jason.nextLine();
    		System.out.println("Enter Address 2: ");
    		addr2 = jason.nextLine();
     
    		//Display user input in formatted view
    		System.out.println("The information you entered is: ");
    		System.out.println("First Name: " + fname);
    		System.out.println("Last Name: " + lname);
    		System.out.println("Address 1: " + addr1);
    		System.out.println("Address 2: " + addr2); 
     
    		//get user confirmation data is valid
    		System.out.println("Is this correct? Y/N ");
    		answer = jason.nextLine();
     
    		}
    }

    But as you can see the next operation is to get the input y/n and if "n", run the first part of the program again and get new input. If "Y", then display something like "thank you your input has been saved." How would something like that look? Back to learning more Java...

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Newbie question - print out strings from input

    For that you use a do while loop.

Similar Threads

  1. Newbie Question...GUI Text input Areas
    By steme in forum Java Theory & Questions
    Replies: 3
    Last Post: April 1st, 2013, 08:45 PM
  2. Print a letter of the alphabet based on a number that is input
    By justlearning in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 01:36 PM
  3. How to print the day of the week from user input?
    By f0x in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2011, 05:33 PM
  4. user input strings to vectors
    By vudoo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 7th, 2011, 10:30 PM
  5. Problems with Input Strings
    By Bekuraryou1228 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 4th, 2010, 04:34 PM