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: Few questions

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Few questions

    This isn't a what is wrong with my code thread. It is a thread about why my program does what it does.

    package example1;
     
    import java.util.Scanner;
     
    public class example1 
    {
     
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner (System.in);
     
            float x;
            int y;
            char ch1, ch2;
            String name;
            String input;
     
            System.out.print("Enter a character: ");
            input = keyboard.next();
            ch1 = input.charAt(0);
            System.out.print("Enter a number: ");
            y = keyboard.nextInt();
            System.out.print("Enter another character: ");
            input = keyboard.next();
            ch2 = input.charAt(0);
            System.out.print("Enter a name: ");
            name = keyboard.next();
            System.out.print("Enter a floating point value: ");
            x = keyboard.nextFloat();
            System.out.println("\nch1 = " + ch1);
            System.out.println("y = " + y);
            System.out.println("ch2 = " + ch2);
            System.out.println("Name is " + name);
            System.out.println("x = " + x);
            System.exit(0);
        }
    }

    There are two parts to this I am just not sure why it does what it does when certain things are input.

    Question 1: Enter x 1234 y john 10.50 (Note: enter all the characters on a single
    press the enter key line and press the enter key). Why did all the prompts after the first
    one run together and not stop for you to enter data?

    Output for Question 1:
    Enter a number: Enter another character: Enter a name: Enter a floating point value: 
    ch1 = x
    y = 1234
    ch2 = y
    Name is john
    x = 10.5

    Question 2: Enter1 89 j 110.50
    press the enter key
    What is the computer waiting for?
    Enter a 1, then explain why the output is the way it is.

    Output for Question 2:
    Enter a number: Enter another character: Enter a name: Enter a floating point value: 1 
    (I entered the 1 to get what's below)
     
    ch1 = 1
    y = 89
    ch2 = j
    Name is 110.50
    x = 1.0
    Last edited by CSUTD; October 11th, 2011 at 09:22 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Few questions

    I figured it out but don't know how to delete a thread.

Similar Threads

  1. JRE and JDK Questions
    By beer-in-box in forum Computer Support
    Replies: 11
    Last Post: September 16th, 2011, 05:37 PM
  2. Many questions
    By SharpT in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 18th, 2011, 09:56 PM
  3. Inheritance questions....
    By smellyhole85 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 06:11 PM
  4. A few questions
    By adenverd in forum Java Theory & Questions
    Replies: 3
    Last Post: May 26th, 2010, 03:34 AM
  5. [SOLVED] Some serious questions,
    By Time in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2010, 02:52 AM