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: Not prompting string user input.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date question

    So I am using the scanner utility for user inputs. There are 3 parts. Integer arithmetic, Real arithmetic and String operations. I am getting desired results with first 2 but I am stuck at the last one.

    Issue-
    String c1 = keyboard.nextLine(); is not prompting for user input. If I use .next instead of .nextLine it prompts but the rest of the string operations are coming out wrong as in the length will only display the length of first word and the not the whole sentence.

    Any help will be appreciated.


    import java.util.Scanner;
     
     
    public class Lab2 {
     
        /**
         * @author
         */
        public static void main(String[] args) {
            int a1,a2,a3;
            System.out.println("*** Test integer arithmetic ***\n");
            Scanner keyboard = new Scanner(System.in);
     
            System.out.print("Enter first integer number: ");
            a1 = keyboard.nextInt();
            System.out.print("Enter second integer number: ");
            a2 = keyboard.nextInt();
            a3 = a1 + a2;
            System.out.print(a1 + " + " );
            System.out.print(a2 + " = " );
            System.out.println(a3);
            a3 = a1 - a2;
            System.out.print(a1 + " - " );
            System.out.print(a2 + " = " );
            System.out.println(a3);
            a3 = a1 * a2;
            System.out.print(a1 + " * " );
            System.out.print(a2 + " = " );
            System.out.println(a3);
            a3 = a1 / a2;
            System.out.print(a1 + " / " );
            System.out.print(a2 + " = " );
            System.out.println(a3);
            a3 = a1 % a2;
            System.out.print(a1 + " % " );
            System.out.print(a2 + " = " );
            System.out.println(a3);
     
            double b1,b2,b3;
            System.out.println("*** Test real arithmetic ***\n");
     
            System.out.print("Enter first integer number: ");
            b1 = keyboard.nextDouble();
            System.out.print("Enter second integer number: ");
            b2 = keyboard.nextDouble();
            b3 = b1 + b2;
            System.out.print(b1 + " + " );
            System.out.print(b2 + " = " );
            System.out.println(b3);
            b3 = b1 - b2;
            System.out.print(b1 + " - " );
            System.out.print(b2 + " = " );
            System.out.println(b3);
            b3 = b1 * b2;
            System.out.print(b1 + " * " );
            System.out.print(b2 + " = " );
            System.out.println(b3);
            b3 = b1 / b2;
            System.out.print(b1 + " / " );
            System.out.print(b2 + " = " );
            System.out.println(b3);
            b3 = b1 % b2;
            System.out.print(b1 + " % " );
            System.out.print(b2 + " = " );
            System.out.println(b3);
     
     
            System.out.println("*** Test String operations ***\n");
            System.out.print("Enter a string of characters: ");
            String c1 = keyboard.nextLine();
            int len = c1.length();
            System.out.println("The length of string \"" + c1 + "\" is " + len);
            System.out.print("Enter an integer between 0 and 20: ");
            int cha = keyboard.nextInt();
            char ch = c1.charAt(cha);
            System.out.println("The character at index \"" + cha + "\" of string \"" + c1 + "\" is " + ch);
            System.out.print("Enter another string of characters: ");
            String c2 = keyboard.next();
            int indx = c1.indexOf(c2);
            System.out.println("The first occurrence of string \"" + c2 + "\" in string \"" + c1 + "\" is at position " + indx );
     
            }
     
    }
    Last edited by Abhi01; April 8th, 2012 at 07:12 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not prompting string user input.

    Nvm. I figured out.

    I just had to add

     keyboard.nextLine();

    before

    String c1 = keyboard.nextLine();

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date Question

    I have another question.

    I am looking for this output -

    Enter your birthday (mm/dd/yyyy): 10/25/1983
    You were born on 25-10-1983

    The user is supposed to input the date in bold.

    String mm, dd, yyyy;
    		System.out.print("Enter your birthday (mm/dd/yyyy): "); 
    		mm = keyboard.next();
    		dd = keyboard.next();
    		yyyy = keyboard.next();
            System.out.println("You were born on " + dd +  mm +  yyyy);

    This the code added to the end of the previous code I posed. With the current code, I am typing in the numbers using the keyboard mm [enter] dd [enter] yyyy [enter].

    This is the output I get : -

    Enter your birthday (mm/dd/yyyy): 01/
    10/
    2001
    You were born on 01/10/2001


    Now I just want to get the 12/10/1989 all in 1 line.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Not prompting string user input.

    I would read it all in one line and then either use String.split to get each section or define explicit positions:

    String bday = keyboard.nextLine();
    String[] strs = bday.split("/");
    System.out.println("You were born on " + strs[1] + "-" + strs[0] + "-" + strs[2]");

    or

    String bday = keyboard.nextLine();
    String d = bday.substring(0,3);
    String m = bday.substring(4,6);
    String y = bday.substring(7,9);
    System.out.println("You were born on " + d + "-" + m + "-" + y");

    You should prefer the first method as it's more reliable.

Similar Threads

  1. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. Parsing a string to an int while receiving user input?
    By happyxcrab in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 05:38 PM
  4. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM
  5. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM

Tags for this Thread