Can anyone help me with this program using the Scanner Class?

I have to write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. Such as 1 hour will equal 40 miles traveled, 2 hours will equal 80, 3 hours will equal 120, etc. This is what I've come up with thus far. Any help is appreciated.
import java.util.Scanner;
     public class DistanceTraveled
          public static void main(String[] args)
               int speed;
               int hours;
               int distance;
               Scanner keyboard = new Scanner(System.in);
               System.out.print("What is the speed of the vehicle in miles-per-hour?");
               speed = keyboard.nextInt();
               System.out.print("How many hours has the vehicle traveled for?");
               hours = keyboard.nextInt();
               distance = speed*hours;
               while (speed < 0)
               while (hours < 1)
               System.out.println("Hour     Distance Traveled");
               System.out.println("------------------------");
               System.out.println(hours + " " + distance);
}

When you post code, wrap it in code tags. Highlight it and click the CODE button above the text input area.
You seem to be trying to reuse the speed and hours variables in your loop. That's probably a mistake at this point. Keep it simpler by defining a loop variable.
Also I don't see the need for two loops. You just want to show how far the vehicle has traveled for each one-hour increment, assuming constant speed, for the number of hours it has been traveling, right? So a single for loop should be sufficient.

Similar Messages

  • Can anyone help me with this program?

    I have to make program that asks the user for information that they would want on a business card. Then, I am supposed ot take that information that was gathered with a listener and display it on a second panel using graphicsstuff (such as g.drawString(VARIABLEHERE, int x, int y). I can get thepart of the program that would ask for the information, but I can't figure out where to go from there on how to display the information. If anyone could help I would be enternally gratefully. This assignment is due Friday morning at 9:00. Thanks!
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class PanelPractice extends JPanel
         private static JButton insert;
         private static JTextField nameField, positionField, areaField, telField, faxField, emailField, add1Field, add2Field, add3Field;
         private static String nameText, positionText, areaText, telText, faxText, emailText, add1Text, add2Text, add3Text;
         public static void main (String[] args)
              //Makes two colored panels that are nested within a third.
              JFrame frame = new JFrame ("Business Card");
              frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
              //Makes the first subpanel
              JPanel subPanel1 = new JPanel();
                   JLabel nameLabel, positionLabel, areaLabel, telLabel, faxLabel, emailLabel, add1Label, add2Label, add3Label;
                   //     Sets up the GUI
                        //Creates labels for the information questions
                        nameLabel = new JLabel ("Type the name you want on the card: ");
                        positionLabel = new JLabel ("Type the person's position: ");
                        areaLabel = new JLabel ("Type the person's area of business: ");
                        telLabel = new JLabel ("Type the person's telephone number: ");
                        faxLabel = new JLabel ("Type the person's fax number: ");
                        emailLabel = new JLabel ("Type the person's e-mail address: ");
                        add1Label = new JLabel ("Type the person's place of business: ");
                        add2Label = new JLabel ("Type the business' street address: ");
                        add3Label = new JLabel ("Type the business' city, state, and zip: ");
                        //Creates a JTextField to hold the person's name
                        nameField = new JTextField (10);
                        positionField = new JTextField (10);
                        areaField = new JTextField (10);
                        telField = new JTextField (10);
                        faxField = new JTextField (10);
                        emailField = new JTextField (10);
                        add1Field = new JTextField (10);
                        add2Field = new JTextField (10);
                        add3Field = new JTextField (10);
                        //add the nameLabel and nameField to the panel
                        subPanel1.add (nameLabel);
                        subPanel1.add (nameField);
                        //add the positionLabel and positionField to the panel
                        subPanel1.add (positionLabel);
                        subPanel1.add (positionField);
                        //add the areaLabel and areaField to the panel
                        subPanel1.add (areaLabel);
                        subPanel1.add (areaField);
                        //add the telLabel and telField to the panel
                        subPanel1.add (telLabel);
                        subPanel1.add (telField);
                        //add the faxLabel and faxField to the panel
                        subPanel1.add (faxLabel);
                        subPanel1.add (faxField);
                        //add the emailLabel and emailField to the panel
                        subPanel1.add (emailLabel);
                        subPanel1.add (emailField);
                        //add the add1Label and add1Field to the panel
                        subPanel1.add (add1Label);
                        subPanel1.add (add1Field);
                        //add the add2Label and add2Field to the panel
                        subPanel1.add (add2Label);
                        subPanel1.add (add2Field);
                        //add the add3Label and add3Field to the panel
                        subPanel1.add (add3Label);
                        subPanel1.add (add3Field);
                        //Creates a button to press to insert the information onto the card
                        insert = new JButton ("Insert Information!");
                        //Creates a Listener and makes it listen for the button to be pressed
                        insert.addActionListener (new ButtonListener());
                        //add the button to the panel
                        subPanel1.add (insert);
                        //set the size of the panel to the width and height constants
                        subPanel1.setPreferredSize (new Dimension (350, 300));
                        //set the color of the panel to whatever you choose
                        subPanel1.setBackground (Color.red);
              //Makes the second subpanel
              JPanel subPanel2 = new JPanel();
              subPanel2.setPreferredSize (new Dimension(500,300));
              subPanel2.setBackground (Color.blue);
              //Makes the primary panel
              JPanel primary = new JPanel();
              primary.setBackground (Color.black);
              primary.add (subPanel1);
              primary.add (subPanel2);
              frame.getContentPane().add(primary);
              frame.pack();
              frame.setVisible(true);
                   //     Represents an action listener for the insert button.
                   private static class ButtonListener implements ActionListener
                        public void actionPerformed (ActionEvent event)
                             //get the text from the textfields
                             nameText = nameField.getText();
                             positionText = positionField.getText();
                             areaText = areaField.getText();
                             telText = telField.getText();
                             faxText = faxField.getText();
                             emailText = emailField.getText();
                             add1Text = add1Field.getText();
                             add2Text = add2Field.getText();
                             add3Text = add3Field.getText();
                   public static class CustomComponent extends JPanel
                   public void paintComponent(Graphics g)
                   super.paintComponent(g);
                   g.drawString(nameText, 5, 5);
    }

    Sorry about that...
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class PanelPractice extends JPanel
    private static JButton insert;
    private static JTextField nameField, positionField, areaField, telField, faxField, emailField, add1Field, add2Field, add3Field;
    private static String nameText, positionText, areaText, telText, faxText, emailText, add1Text, add2Text, add3Text;
    public static void main (String[] args)
    //Makes two colored panels that are nested within a third.
    JFrame frame = new JFrame ("Business Card");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    //Makes the first subpanel
    JPanel subPanel1 = new JPanel();
    JLabel nameLabel, positionLabel, areaLabel, telLabel, faxLabel, emailLabel, add1Label, add2Label, add3Label;
    // Sets up the GUI
    //Creates labels for the information questions
    nameLabel = new JLabel ("Type the name you want on the card: ");
    positionLabel = new JLabel ("Type the person's position: ");
    areaLabel = new JLabel ("Type the person's area of business: ");
    telLabel = new JLabel ("Type the person's telephone number: ");
    faxLabel = new JLabel ("Type the person's fax number: ");
    emailLabel = new JLabel ("Type the person's e-mail address: ");
    add1Label = new JLabel ("Type the person's place of business: ");
    add2Label = new JLabel ("Type the business' street address: ");
    add3Label = new JLabel ("Type the business' city, state, and zip: ");
    //Creates a JTextField to hold the person's name
    nameField = new JTextField (10);
    positionField = new JTextField (10);
    areaField = new JTextField (10);
    telField = new JTextField (10);
    faxField = new JTextField (10);
    emailField = new JTextField (10);
    add1Field = new JTextField (10);
    add2Field = new JTextField (10);
    add3Field = new JTextField (10);
    //add the nameLabel and nameField to the panel
    subPanel1.add (nameLabel);
    subPanel1.add (nameField);
    //add the positionLabel and positionField to the panel
    subPanel1.add (positionLabel);
    subPanel1.add (positionField);
    //add the areaLabel and areaField to the panel
    subPanel1.add (areaLabel);
    subPanel1.add (areaField);
    //add the telLabel and telField to the panel
    subPanel1.add (telLabel);
    subPanel1.add (telField);
    //add the faxLabel and faxField to the panel
    subPanel1.add (faxLabel);
    subPanel1.add (faxField);
    //add the emailLabel and emailField to the panel
    subPanel1.add (emailLabel);
    subPanel1.add (emailField);
    //add the add1Label and add1Field to the panel
    subPanel1.add (add1Label);
    subPanel1.add (add1Field);
    //add the add2Label and add2Field to the panel
    subPanel1.add (add2Label);
    subPanel1.add (add2Field);
    //add the add3Label and add3Field to the panel
    subPanel1.add (add3Label);
    subPanel1.add (add3Field);
    //Creates a button to press to insert the information onto the card
    insert = new JButton ("Insert Information!");
    //Creates a Listener and makes it listen for the button to be pressed
    insert.addActionListener (new ButtonListener());
    //add the button to the panel
    subPanel1.add (insert);
    //set the size of the panel to the width and height constants
    subPanel1.setPreferredSize (new Dimension (350, 300));
    //set the color of the panel to whatever you choose
    subPanel1.setBackground (Color.red);
    //Makes the second subpanel
    JPanel subPanel2 = new JPanel();
    subPanel2.setPreferredSize (new Dimension(500,300));
    subPanel2.setBackground (Color.blue);
    //Makes the primary panel
    JPanel primary = new JPanel();
    primary.setBackground (Color.black);
    primary.add (subPanel1);
    primary.add (subPanel2);
    frame.getContentPane().add(primary);
    frame.pack();
    frame.setVisible(true);
    // Represents an action listener for the insert button.
    private static class ButtonListener implements ActionListener
    public void actionPerformed (ActionEvent event)
    //get the text from the textfields
    nameText = nameField.getText();
    positionText = positionField.getText();
    areaText = areaField.getText();
    telText = telField.getText();
    faxText = faxField.getText();
    emailText = emailField.getText();
    add1Text = add1Field.getText();
    add2Text = add2Field.getText();
    add3Text = add3Field.getText();
    public static class CustomComponent extends JPanel
    public void paintComponent(Graphics g)
    super.paintComponent(g);
    g.drawString(nameText, 5, 5);
    } No..I'm not expecting someone to do it for me. I am having trouble figuring out what to do next. I cannot get anything to show up on the second panel...the part that displays the information that the listener gathered.
    If I could figure out how to get one thing to show up...then I could probably do the rest...it's getting it started that I can't get.

  • I can't find the 2G, 3G and LTE options after updating to iOS8.1, can anyone help me with this? I'm using iPhone5s

    I can't find the 2G, 3G and LTE options after updating to iOS8.1, can anyone help me with this? I'm using iPhone5s

    I have check on my service provider about the correct APN settings, but unfortunately doesnt work still..

  • When my screen saver has been on a while, I have to use the cursor to "rub out" part of the screen saver to see my login section. Can anyone help me with this?

    When my screen saver has been on a while, I have to use the cursor to "rub out" part of the screen saver to see my login section. Can anyone help me with this?

    I don't have a solution for you, but just wanted to let you know I used to have the exact same problem. I resolved it (for the most part....about 90% of the time, anyway) by adjusting the screensaver and power settings to never let the computer go to sleep.

  • TS3276 I am not able to Mobile Me email account to connect with iCloud or my Apple Mail program. Don't know if my settings are correct or not. Can anyone help me with this please?

    I am not able to Mobile Me email account to connect with iCloud or my Apple Mail program. Don't know if my settings are correct or not. Can anyone help me with this please?

    http://support.apple.com/kb/HT5922
    If you want to mirror your desktop, see:
    http://support.apple.com/kb/HT5404
    Regards.

  • I just received photoshop elements 10 that I bought used thru Ebay, for my OSX Mac 10.5.8. I put the discs in my computer and nothing happens to help me download it. Can anyone help me with this instillation challenge?

    I just received photoshop elements 10 that I bought used thru Ebay, for my OSX Mac 10.5.8. I put the discs in my computer and nothing happens to help me download it. Can anyone help me with this instillation challenge?

    Does your mac have an intel processor or PowerPC processor?
    System requirements | Adobe Photoshop Elements
    Did you insert the disk that is labeled mac os?
    After you insert the disk, it should show on your desktop.
    If you double click on the the disk icon on the desktop, does anything happen?
    more info about the included disks:
    FAQ: Installing Elements 10, or What do all these disks do?

  • I'm having trouble with the volume of my i phone 4s when using headphones, the sound will only appear when the volume is at its highest, any lower and all I hear is static. Can anyone help me with this problem? Much appreciated.

    Hi, I'm using a white iPhone 4s with 16gb memory and having some problems. Whenever I use earphones the sound will be heated when the volume is at the highest if I turn it down any lower all I hear is static. Can anyone help me with this problem as I would appreciate it much.

    Reset the device:
    Press and hold the Sleep/Wake button and the Home button together for at least ten seconds, until the Apple logo appears.
    If that doesn't help, tap Settings > General > Reset > Reset All Settings
    No data is lost due to a reset.

  • HT1365 Hi can anyone help with this issue regarding my wireless magic mouse? When im on google chrome and scrolling down the page i always have youtube running in the background but the audio cuts/spits/pops can anyone help me with this?

    Hi can anyone help with this issue regarding my wireless magic mouse? When im on google chrome and scrolling down the page i always have youtube running in the background but the audio cuts/spits/pops can anyone help me with this?

    The figures you mention only make sense on your intranet.  Are you still using the same wireless router.  The verizon one is somewhat limited as far as max wireless-n performace.  For one thing it only has a 2.4 radio.   I like many people who wanted wireless-n performance before they even added a wireless-n gigabit router, have my own handling my wireless-n network.

  • HT201210 (The iPhone "Named" cannot be restored at this time because the iPhone software update server could not be contacted or is temporarily unavailable.) can anyone help me with this issue, every time i try to restore it will come up like this error m

    (The iPhone "Named" cannot be restored at this time because the iPhone software update server could not be contacted or is temporarily unavailable.)
    can anyone help me with this issue, every time i try to restore it will come up like this error message.

    Restore the iPhone when connected to iTunes by cable.
    Still the same TS1275?
    Is your iPhone jailbroken?
    Or
    Has your computer ever been used to jailbrake or downgrade (Tinyumbrella) any iPhone?

  • I have updated to the new version of iPhoto but it will no longer let me edit text in the preset text boxes.  The text is highlighted for a nanosecond but will not stay highlighted so I can amend it.  Can anyone help me with this.  Many thanks.

    I have updated to the latest version of iPhoto.  But now iPhoto will not let me amend the preset text in the text boxes.
    the text is highlighted for a nano second but it will not stay highlighted so that I can add my own text.
    can anyone help me with this issue.
    Many thanks

    You might try using the add-on 'NoSquint' which allows numerous zoom options specific to each page you visit & keeps your settings - https://addons.mozilla.org/en-US/firefox/addon/nosquint/
    If you want to go back to 3.6x, you will find it here:
    http://www.mozilla.com/en-US/firefox/all-older.html
    In most cases you can simply "upgrade" (meaning downgrade) directly from the installation. It would be a good idea to save your passwords & bookmarks just to be on the safe side.

  • HT1277 Mail on my Mac computer does not update when I update my mail on my phone and iPad.  Can anyone help me with this?  Is there a setting I need to check?

    Mail on my Mac computer does not update when I update my mail on my iPhone and iPad. Can anyone help me with this?  Is there a setting that I need to check?

    All that you had to do was to sign into the old account in order to update those apps. What I mean is that you just needed to sign into that account in the store settings like I described and that should have worked. You didnt need to enter the credit card information again - you justed needed to use the old ID and password.
    Anyway, I think the good news is that if everything else is OK with the new account, just download iBooks with the new ID - it's a free app so its not like you have to pay for it again. I'm not sure what the other App is that you are talking about - but if it is the Apple Store App - that is free as well.
    Try this anyway, when you try to update iBooks, just use the old password if the old ID still pops up.
    Did you try signing into the store settings with your new ID and see what happens with the updates then?

  • HT1933 I am facing problems with mails after update iPhone 4S/io7. I have configured yahoo mail in iPhone 4S. When I click on refresh, it shows new mail downloaded but when I go inside inbox the new mail don't show up. Can anyone help me with this iOS 7 i

    I am facing problems with mails after update iPhone 4S/io7. I have configured yahoo mail in iPhone 4S. When I click on refresh, it shows new mail downloaded but when I go inside inbox the new mail don't show up. Can anyone help me with this iOS 7 issue ?

    Hi were you able to resolve yours?
    I was very worried losing all my medical stuff on my iPad Air 2, plus i have nothing to use for the next day! So i have to try all the possible things i read on the internet!! And this worked on my part (Thank God, and thank you Google)
    Perform a hard reboot
    Hold down the Home button and Power button on your iPhone or iPad and keep holding them until you see the Apple logo. Once you do, you can let go. When your device boots back up, go ahead and check to see if the apps have started re-downloading. If they still appear to be stuck, continue on.

  • When I enter Yahoo.ca, I lose my internet connection(safari). Can anyone help me with this. Thanks.

    When I enter Yahoo.ca, I lose my internet connection. This happens most of the time. Sometimes immediatly, other times after a few minutes. Can anyone help me with this? Thanks.

    All that you had to do was to sign into the old account in order to update those apps. What I mean is that you just needed to sign into that account in the store settings like I described and that should have worked. You didnt need to enter the credit card information again - you justed needed to use the old ID and password.
    Anyway, I think the good news is that if everything else is OK with the new account, just download iBooks with the new ID - it's a free app so its not like you have to pay for it again. I'm not sure what the other App is that you are talking about - but if it is the Apple Store App - that is free as well.
    Try this anyway, when you try to update iBooks, just use the old password if the old ID still pops up.
    Did you try signing into the store settings with your new ID and see what happens with the updates then?

  • HT201413 i have an iphone 3gs and everytime i try to restore it on itunes it just keeps saying an unknown erroe has occured (29) can anyone help me with this

    i have an iphone 3gs and everytime i try to restore it on itunes it just keeps saying an unknown erroe has occured (29) can anyone help me with this??

    Hello Laura,
    Thank you for posting in the Apple Support Communities.  I found an article that has information about Error 29:
    Error 20, 21, 23, 26, 28, 29, 34, 36, 37, 40
    These errors typically occur when security software interferes with the restore and update process. Follow Troubleshooting security software issues to resolve this issue. In rare cases, these errors may be a hardware issue. If the errors persist on another computer, the device may need service.
    Also, check your hosts file to verify that it's not blocking iTunes from communicating with the update server. See iTunes: Advanced iTunes Store troubleshooting—follow steps under the heading Blocked by configuration (Mac OS X / Windows) > Rebuild network information > Mac OS X > The hosts file may also be blocking the iTunes Store. If you have software used to perform unauthorized modifications to the iOS device, uninstall this software prior to editing the hosts file to prevent that software from automatically modifying the hosts file again on restart.
    You can find the full article here:
    iTunes: Specific update-and-restore error messages and advanced troubleshooting
    http://support.apple.com/kb/ts3694
    Best,
    Sheila M.

  • Want to install Skydrive on my mac, but the app store wants US billing information and I am in Gibraltar. Can anyone help me with this?

    Want to install Skydrive on my mac, but the app store wants US billing information and I am in Gibraltar. Can anyone help me with this?

    Yes, if this is a permanent change. But you still have to have all the other changes - billing address, credit card, residence, etc. See Manage My Apple ID.
    If you can't remember answers to security questions:
    The Three Best Alternatives for Security Questions and Rescue Mail
        1. Use Apple's Express Lane.
              Go to https://expresslane.apple.com ; click 'See all products and services' at the
              bottom of the page. In the next page click 'More Products and Services, then
              'Apple ID'. In the next page select 'Other Apple ID Topics' then 'Forgotten Apple
              ID security questions' and click 'Continue'. Please be patient waiting for the return
              phone call. It will come in time depending on how heavily the servers are being hit.
         2.  Call Apple Support in your country: Customer Service: Contact Apple support.
         3.  Rescue email address and how to reset Apple ID security questions.
    A substitute for using the security questions is to use 2-step verification:
    Two-step verification FAQ Get answers to frequently asked questions about two-step verification for Apple ID.

Maybe you are looking for

  • Why can't I play AUTHORIZED content through Home Sharing?

    I have 2 laptops, one is a doomed Win and the other is a blessed Mac. I tried home sharing for the first time and to back up my content to my Mac. Both are authorized to play the same content, HOWEVER, some TV shows just won't play (and of course if

  • Unit of Issue vs UoM

    Hi Gurus We have a scenario, where in we are maintaining , diff  UoM for the Base unit & unit of issue in the material master. Our legacy team finds it easy to maintain the stock in unit of issue. how will it impact initial stock loading ? Muthu

  • Web Service Logs

    Hi all          i am doing a web service -> XI  -> R3( RFC)  Scenario.my scenario is running,  i had to use <b> Altova XML Spy</b> for calling web service .... now the issue is, <b>can i keep track of who has called that webservice</b>.. ex :- can i 

  • Won't accept my three digit security code

    My card number, valid thru date, and type are all correct, but every time I put it in it says it needs a valid security code. Right now if I wanted to use my card, I can't (red), but that doesn't mean my card isn't still correct.  Is this the problem

  • High Level of Frustration on This Board With CS4

    There is a very apparent level of high frustation evident on this board among the various message strings regarding the terrible overall performance of CS4. There are so many bugs reported I just can't keep track. We have people insulting each other