Results 1 to 7 of 7
Thread: while loop not working
- 06-18-2010, 10:13 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
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:
THE OUTPUT I GET IS :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
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
- 06-18-2010, 10:31 AM #2
write
console.nextLine();
before
model[k]=console.nextLine();http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)First, solve the problem. Then, write the code.
- 06-18-2010, 11:08 AM #3
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:
- 06-20-2010, 04:32 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@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.
- 06-20-2010, 04:46 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also avoid unnecessary declarations.
Just think about what really dose that console.nextLine();, how it if your issue?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(); }
- 06-20-2010, 10:58 PM #6
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 codeTeaching myself java so that i can eventually join the industry! Started in June 2010
- 06-21-2010, 07:29 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also it's clear to define the size of the arrays as a constant variable. It's much clear as well.
Similar Threads
-
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
working with JC
By yuhobebbho in forum New To JavaReplies: 0Last Post: 02-10-2010, 11:22 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Working With ANT
By JavaForums in forum EclipseReplies: 0Last Post: 04-26-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks