There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Java question...


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
charliekim's Avatar
Senior Member with 140 posts.
 
Join Date: Jun 2002
16-Mar-2003, 05:03 PM #1
Java question...
i am setting up a program that inputs 2 user data through input dialog boxes from JOptionPane. i am using a sentinel value control that indicates when the user enters in a sentinel value set to -1, it will calculate the data inputted. this is what it looks like so far..

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

//beginning class

public class Gas {

//beginning method

public static void main(String args[])

{

//declaring variables

int miles, gallons, totalmiles, totalgallons, counter;

double averagemiles, totalaverage;

String usermiles, gallonmiles, result;

//initialization

totalmiles = 0;
totalgallons = 0;
counter = 0;
averagemiles=0;
totalaverage=0;

//obtaining user inputs

usermiles = JOptionPane.showInputDialog("Enter Miles Driven or -1 to quit");

gallonmiles = JOptionPane.showInputDialog("Enter Gallons Used");

//parseing from string to integers

miles = Integer.parseInt(usermiles);

gallons = Integer.parseInt(gallonmiles);

//loop until sentinel value read from user

while (miles != -1){

//getting next user input

usermiles = JOptionPane.showInputDialog("Enter Miles Driven or -1 to quit");

gallonmiles = JOptionPane.showInputDialog("Enter Gallons Used");

miles = Integer.parseInt(usermiles); //parseing from string to integers

gallons = Integer.parseInt(gallonmiles);

totalmiles = totalmiles + miles;

totalgallons = totalgallons + gallons;

counter = counter + 1;

//end while

}

//termination phase

DecimalFormat twoDigits = new DecimalFormat("0.00");

if (counter != 0){

// calculate the averages

averagemiles = (double)miles / (double)gallons;

totalaverage = totalmiles / totalgallons;

}

//diplaying result

result = ("Average Miles/Gallon : " + averagemiles
+ "\nTotal Miles/Gallon : " + totalaverage);

JOptionPane.showMessageDialog(null, result, "Mileage", JOptionPane.INFORMATION_MESSAGE);

//terminate program

System.exit(0);

//ending method main

}

//ending class


}


for some reason, when i enter -1 to stop the second while loop, it still asks for the gallons used input box. it should by pass the whole while loop when the user enters sentinel value -1.

anyone have any ideas..
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:45 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.