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: Java user input

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java user input

    Okay im trying to get when a user inputs a question the java program will output a pre written answer. I have the following code


    import java.util.Scanner;
     
     
    public class Main {
    	public static void main(String[] args){
    	Scanner user_input = new Scanner( System.in );
     
    	String username;
    	System.out.print("Username ");
    	username = user_input.next();
     
     String user_input1;
     user_input1 = ("Admin");
    			System.out.println("Welcome Admin");
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Java user input

    You say "Okay im trying to get when a user inputs a question the java program will output a pre written answer."

    Are you only expecting one question?

    Also . . . change

    username = user_input.next();

    to


    username = user_input.nextLine();

    I'd consider two things: If you only require one entry:

    import java.util.Scanner;
     
     
    public class Main {
    	public static void main(String[] args){
    	Scanner user_input = new Scanner( System.in );
     
    	String username;
    	System.out.print("Username: ");
    	username = user_input.nextLine();
     
     String user_input1;
     user_input1 = ("Admin");
     System.out.println(username);			
     System.out.println("Welcome Admin");
     
    	}
    }

    But I think you are probably talking about more than one possible question (entry).

    In that case, you'd be better off looking at using an ArrayList
    to store your entries and responses. Then you can check the
    arraylist for an entry, and provide the corresponding response.

  3. The Following User Says Thank You to Skywola For This Useful Post:

    Camwarp (January 4th, 2014)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java user input

    I am trying to make it into a multiple list multiple questions type thing but thanks ill take a look into that

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    Chandler, Arizona, USA
    Posts
    25
    My Mood
    Devilish
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Java user input

    I created a project similar to what you are talking about. ArrayList works good for it, but you will want to create a class for the questions and answers. Creating a class and passing it to the arrraylist will save you a whole lot of headaches regarding how to keep the questions and answers organized.

    You will want something to the effect of:

    import java.util.ArrayList;
     
    public class QnA {
            strQuestion = "";
            strAnswer = "";
    }
     
    QnA qna = new QnA();
    ArrayList<QnA> qnaArrayList = new ArrayList<QnA>();
     
    qna.strQuestion = "Who is the wildest coyote in the world?";
    qna.strAnswer = "Wilie Coyote, of course!!";
     
    qnaArrayList.add(qna);

    This is just a cobbled together code piece, I did not test it, I don't want to do
    all of your work for you. One thing you NEED to remember though.
    Each time you add a question and answer pair to the ArrayList, you need
    to create a NEW qna class . . . . as in . . . . QnA qna = new QnA();
    If you do not do that, you will just be over-writing the first one you
    put in.

    You will find it MUCH easier passing a class to the arraylist
    than trying to handle the questions and answers individually!
    Also, you can modify the class and add more features.
    I added file information, correct answer info, and info about
    the questions in a header on mine. There is an easy way
    to randomize too if you use a class type.

Similar Threads

  1. BEGINNER: Asking user input from java/class to jsp
    By fmjnax in forum JavaServer Pages: JSP & JSTL
    Replies: 9
    Last Post: October 29th, 2013, 11:50 PM
  2. Java user input troubles using the scanner class
    By Lach in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2013, 11:13 AM
  3. User Input/Output for Math Calculations - Java
    By Infinite in forum Java Theory & Questions
    Replies: 7
    Last Post: July 9th, 2013, 03:45 AM
  4. Java User-Input bug (not continuing); NOVICE
    By Shzylo in forum Java Theory & Questions
    Replies: 11
    Last Post: December 14th, 2012, 07:54 AM
  5. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM