Results 1 to 7 of 7
  1. #1
    RBNSN83 is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default while loop not working

    This code is used to store data into three different arrays. The desired output will be a table where the contents of the arrays will be the three different columns. Like this:
    *****************************
    Vehicle Type Year Model
    *****************************
    var 1980 v
    lorry 1982 b
    boat 1972 c
    *****************************
    This is the following code that I wrote to display such an output:
    Java Code:
    package trycardatabase;
    import java.util.Scanner;import java.io.*;
    /**
     *
     * @author c1272e
     */
    public class CarDataBase {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            //Declare three differrent arrays
            String [] car = new String[3];
            int [] year = new int[3];
            String [] model = new String[3];
    
            //variable declaration
            System.out.println("Your about to enter into the loop to fill up the database.");
    
            //Scanner declaration
            Scanner console = new Scanner(System.in);
            Scanner con = new Scanner(System.in);
    
            //The loop itself
            for(int i = 0; i<3; i++){
                System.out.println("Row number:\t"+i);
                System.out.println("1.Enter the vehicle type:");
                car[i]= console.nextLine();
                int j = i;
                System.out.println("2.Enter the year it was manufactured:");
                                year[j]=console.nextInt();
                int k = j;
                [COLOR="Blue"]System.out.println("3.Enter the model:");
                model[k]=console.nextLine();[/COLOR]            
                
            }
            /*The following lines would display the results in the code*/
            System.out.println("Output the results in the arrays");
            System.out.println("*****************************");
            System.out.print("Vehicle Type\tYear\tModel\n");
            System.out.println("*****************************");
            for(int l = 0; l<3; l++){
                System.out.println(car[l]+"\t\t\t\t\t"+year[l]+"\t\t\t\t\t"+model[l]);
            }
            System.out.println("*****************************");
        }
    
    
    }//end of code
    THE OUTPUT I GET IS :

    run:
    Your about to enter into the loop to fill up the database.
    Row number: 0
    1.Enter the vehicle type:
    var
    2.Enter the year it was manufactured:
    1980
    3.Enter the model:
    Row number: 1
    1.Enter the vehicle type:
    lorry
    2.Enter the year it was manufactured:
    1982
    3.Enter the model:
    Row number: 2
    1.Enter the vehicle type:
    boat
    2.Enter the year it was manufactured:
    1972
    3.Enter the model:
    Output the results in the arrays
    *****************************
    Vehicle Type Year Model
    *****************************
    var 1980
    lorry 1982
    boat 1972
    *****************************
    BUILD SUCCESSFUL (total time: 1 minute 17 seconds)
    ************************************************** ********
    You see the loop is executing fine but some how the line where the
    model[k]=console.nextLine();
    is not waiting to take an output from the keyboard. It prints "3.Enter the model:" But it does not wait to accept the input. somehow this line is not functioning.
    Last edited by Eranga; 06-20-2010 at 04:30 PM. Reason: code tags added

  2. #2
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    write
    console.nextLine();

    before
    model[k]=console.nextLine();
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    year[j]=console.nextInt();
    int k = j;
    console.nextLine();
    System.out.println("3.Enter the model:");
    model[k]=console.nextLine();

    add the bolded one after receiving the integer input as the enter key will not be pressed once if the integer is received.we have to forcefully move to next line to accept the next input.
    Ramya:cool:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP, please use code tags next time posting your code segment. Unformated code tags are really hard to read. If you really don't know how it could be done, check my forum signature.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also avoid unnecessary declarations.

    Java Code:
            for (int i = 0; i < 3; i++) {
                System.out.println("Row number:\t" + i);
                
                System.out.println("1.Enter the vehicle type:");
                car[i] = console.nextLine();
    
                System.out.println("2.Enter the year it was manufactured:");
                year[i] = console.nextInt();
    
                console.nextLine();
                System.out.println("3.Enter the model:");
                model[i] = console.nextLine();
            }
    Just think about what really dose that console.nextLine();, how it if your issue?

  6. #6
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Heres your code but made a bit more cleaner.

    PHP Code:
    import java.util.Scanner;
    
    public class CarDataBase {
    
        public static void main(String[] args) {
    
            int CAR_AMOUNT = 3;
    
            String [] car = new String[CAR_AMOUNT];
            int [] year = new int[CAR_AMOUNT];
            String [] model = new String[CAR_AMOUNT];
    
            //variable declaration
            System.out.println("Your about to enter into the loop to fill up the database.");
    
            //Scanner declaration
            Scanner console = new Scanner(System.in);
    
            //The loop itself
            for(int i = 0; i<CAR_AMOUNT ; i++){
                System.out.printf("Row number:\t %s \n 1.Enter the vehicle type: \n ",i);
                car[i]= console.nextLine();
    
                System.out.println("2.Enter the year it was manufactured:");
                year[i]=console.nextInt();
    
                System.out.println("3.Enter the model:");
                console.nextLine();
                model[i]=console.nextLine();
    
            }
            /*The following lines would display the results in the code*/
            String starBorder = "*****************************";
            System.out.println("Output the results in the arrays");
            System.out.println(starBorder);
            System.out.print("Vehicle Type\tYear\tModel\n");
            System.out.println(starBorder);
            for(int i = 0; i<CAR_AMOUNT ; i++){
                System.out.printf("%s \t %s \t %s \n",car[i],year[i],model[i]);
            }
            System.out.println(starBorder);
        }
    }//end of code
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  2. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

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