Results 1 to 3 of 3
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default My codes are not working functionally

    Hello, I create a program which asks user to enter name, middle name, last name, street number, street name, city, state and zip code, then show result.
    Everything works fine until street name, program show question but pass to the next question which is "Enter your city" to get input, it does not allow to enter street name. here is the codes please take a look at and help me;

    import java.util.Scanner;

    public class Program1
    {

    public static void main(String[] args)
    {
    String firstName;
    String middleInitial;
    String lastName;
    int streetNumber;
    String streetName;
    String city;
    String state;
    int zipCode;

    Scanner in;
    in = new Scanner (System.in);

    System.out.println("Enter your first name: ");
    firstName = in.nextLine();

    System.out.println("Enter your middle name: ");
    middleInitial = in.nextLine();
    char middleInitialChar = middleInitial.charAt(0);

    System.out.println("Enter your last name: ");
    lastName = in.nextLine();

    System.out.println("Enter your street number: ");
    streetNumber = in.nextInt();

    System.out.println("Enter your street name: ");
    streetName = in.nextLine();

    System.out.println("Enter your city: ");
    city = in.nextLine();

    System.out.println("Enter your state: ");
    state = in.nextLine();

    System.out.println("Enter your zip code: ");
    zipCode = in.nextInt();

    System.out.println(firstName + " " + middleInitialChar + ". " + lastName );
    System.out.println(streetNumber + " " + streetName );
    System.out.println(city + ", " + state + " " + zipCode );
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    After your Scanner gets the nextInt, it finds the next line symbol and uses it without your wanting it to. You should explicitly tell it to swallow the end of line symbol after getting the int like so:

    Java Code:
          System.out.println("Enter your street number: ");
          streetNumber = in.nextInt();
          in.nextLine(); // **** add this to get the Scanner to swallow the end of line symbol

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck!

  3. #3
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks for helping, I will keep in mind your advice and try to do it on my next post, I am new at posting and also interface

Similar Threads

  1. two short codes
    By Libertyman in forum New To Java
    Replies: 7
    Last Post: 06-21-2010, 03:22 PM
  2. Assistant on my codes. SOS!!
    By sya1912 in forum Java Applets
    Replies: 16
    Last Post: 09-01-2008, 02:23 PM
  3. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 03:57 AM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •