473,840 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP on while loops problems

6 New Member
Hi,
I'm beginner at Java. I'm confused with my simple program of using while loops

int i = 1;
while (i <= input){
System.out.prin tln("Enter description of product #" + i + " : ");
item = keyboard.nextLi ne();

System.out.prin tln("Enter price of " + item + " : ");
price = keyboard.nextDo uble();
i++;
}

I just wondering why when i run the program, the output will be:

Enter description of product #1 :
Enter price of :
3
Enter description of product #2 :
Enter price of :
2

and while i change a bit to

for (int i = 1; i <= input; i++){
System.out.prin tln("Enter description of product #" + i + " : ");
item = keyboard.nextLi ne();
}

the output will be:

Enter description of product #1 :
Enter description of product #2 :
3

really need helpp...

Many thanks.
Apr 12 '08 #1
8 1554
Dököll
2,364 Recognized Expert Top Contributor
Hi,
I'm beginner at Java. I'm confused with my simple program of using while loops

Expand|Select|Wrap|Line Numbers
  1.  
  2. int i = 1;
  3.         while (i <= input){
  4.             System.out.println("Enter description of product #" + i + " : ");
  5.             item = keyboard.nextLine();
  6.  
  7.             System.out.println("Enter price of " + item + " : ");
  8.             price = keyboard.nextDouble();
  9.             i++;
  10.         }
  11.  
  12. I just wondering why when i run the program, the output will be:
  13.  
  14. Enter description of product #1 : 
  15. Enter price of  : 
  16. 3
  17. Enter description of product #2 : 
  18. Enter price of  : 
  19. 2
  20.  
  21. and while i change a bit to
  22.  
  23. for (int i = 1; i <= input; i++){
  24.             System.out.println("Enter description of product #" + i + " : ");
  25.             item = keyboard.nextLine();
  26. }
  27.  
  28.  
the output will be:

Enter description of product #1 :
Enter description of product #2 :
3

really need helpp...

Many thanks.
Hey there, is there additional class/code feeding into this one. Also, and I can be way off, I am still an apprentice; you should probably initialize from zero here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int i = 0;
  3.         while (i = 0; i <= input; i++){
  4.  
  5.  
Do you have to start at one?

In a bit!

Dököll
Apr 12 '08 #2
blazzer
6 New Member
Hey there, is there additional class/code feeding into this one. Also, and I can be way off, I am still an apprentice; you should probably initialize from zero here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int i = 0;
  3.         while (i = 0; i <= input; i++){
  4.  
  5.  
Do you have to start at one?

In a bit!

Dököll
if i start from zero, the product # will start from 0 too..
it will print product #0 instead of 1.

i'm confused that why it doesnt ask for the first input first then jump to second one..
i cant enter product describtion..it will jump to price right away...
any idea?

many thanks...
Apr 12 '08 #3
blazzer
6 New Member
apperantly i've changed using "for" loops
but it doesn't change at all..

for (int i = 1; i <= input; i++){
System.out.prin tln("Enter description of product #" + i + " : ");
item = keyboard.nextLi ne();

System.out.prin tln("Enter price of " + item + " : ");
price = keyboard.nextDo uble();
total_Price += price;

}

is there any problem there? it works fine when asking first one..but once it loops, it will skip the description..
any idea?

many thanks

blazzer
Apr 12 '08 #4
sukatoa
539 Contributor
if i start from zero, the product # will start from 0 too..
it will print product #0 instead of 1.

i'm confused that why it doesnt ask for the first input first then jump to second one..
i cant enter product describtion..it will jump to price right away...
any idea?

many thanks...
I can't get how do you like to have the output....
Can you post some output?

And also, your expected correct output....

Just confirming,
sukatoa
Apr 12 '08 #5
blazzer
6 New Member
I can't get how do you like to have the output....
Can you post some output?

And also, your expected correct output....

Just confirming,
sukatoa
this is the current output:

How many items the customer is purchasing:
2
Starting new customer...
Enter description of product #1 :
bread
Enter price of bread :
12
Enter description of product #2 :
Enter price of :
12

I'm expected the output will ask for the description first and jump to price.

but the actual output is..when reach to second loops and more, it doesnt ask for the description, it jump to price without asking for description.

The problem is..after looping.
Apr 12 '08 #6
sukatoa
539 Contributor
this is the current output:

How many items the customer is purchasing:
2
Starting new customer...
Enter description of product #1 :
bread
Enter price of bread :
12
Enter description of product #2 :
Enter price of :
12

I'm expected the output will ask for the description first and jump to price.

but the actual output is..when reach to second loops and more, it doesnt ask for the description, it jump to price without asking for description.

The problem is..after looping.
Since you are using nextLine(), the value after the previous input in that current line will automatically captured when it invoke at the next input........ i don't know what character it is....

Because, invoking nextLine() will seek for "\n" for the end of the input....

use next()... try to observe.....

sukatoa
Apr 12 '08 #7
sukatoa
539 Contributor
You can do the test at your original post... the while loop...

regards,
sukatoa
Apr 12 '08 #8
JosAH
11,448 Recognized Expert MVP
A nextDouble() call stops reading at the character that can't be the last part of
a String representation of a double number and leaves it in the input buffer.

Note that an end of line character can never be part of s String representation of
a double number.

A nextLine() call reads everything including the end of line character from an
input buffer. Given these three observations reread the original code: the loops
use different methods: nextDouble and nextLine.

kind regards,

Jos
Apr 12 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1706
by: Donna | last post by:
I am attempting a distance learning course in Java but am having problems with a task. I have completed the simple little program as listed below. public class Results { private static String version = "Module Results (version Exercise 1-3)"; static Group myGroup = new Group();
147
10199
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
2
2302
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
3
2425
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is there a way of shorterning this? freeing up the memory say, bcos what is actually displayed is not...
2
1843
by: shblack | last post by:
I need help with a program I am writing for school. The program has to do the following: /* 1. Character string must be a minimium of 15 charactors. 2. If not 15 characters long it must give error and send you back to orginal question. 3. After entering 15 characters it must count the CAPTIAL letters and tell you how many there are. 4. Need a loop to count and produce error and take you back to first screen. */
15
2588
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
2
1664
by: Toby | last post by:
I'm trying to write a simple commandline wrapper: a script that runs another program as a child and relays unbuffered stdin and stdout to/from the child process, possibly filtering it. The usefulness of such a program lies in the filtering stage, in a possible integration with readline, or in other interface enhancements. Still, I'd like to discuss with you the unfiltered, unembellished version, because I'm not satisfied with it and...
1
1500
by: jiggaman828 | last post by:
Am I doing something wrong with setting my width? I thought I had it correct. Can any inform me on how to write 4 different while or do-while loops counting down to 0, it has to be lined up column by column. Example: Days hours Minutes ------- --------- ------------ 0 96 382 1 72 . 2 48 . 3 24 . .
8
1900
by: Dameon99 | last post by:
my program compiles without problems but when i try to run it pauses shortly and then crashes. When i set it to debug it came up with the following message: "An Access Violation (Segmentation Fault) raised in your program." Ill put my code below. can anyone see why it is doing this? /* Program Function: Reads in data on a boolean matrix (/s) and outputs * whether the file has parity, is corrupt or the coordinates of the bit...
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9436
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.