Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: What am I missing?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What am I missing?

    My code compiles and runs, but it doesn't execute as many times as I need it to. I need it to ask for input 12 times (for each month). What am I missing? Here's the code:

    [/highlight = Java]
    import java.util.Scanner;

    public class HighestTemp {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter the temperature for the month:");
    double highestValue = in.nextDouble();
    int highestMonth = 1;
    for (int currentMonth = 2; currentMonth <= 12; currentMonth++) {
    double nextValue = in.nextDouble();
    if (nextValue > highestValue) {
    highestValue = nextValue;
    highestMonth = currentMonth;

    }
    System.out.println("The highest value is: " + highestValue +
    " and the month of the highest value is: " + highestMonth);

    }
    }
    }
    [/highlight]

    Thanks for any help!!!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: What am I missing?

    I need it to ask for input 12 times
    Your code is only asking for input once (before the start of the for loop). If you want or intend it to ask multiple times you had better have it also - or instead - ask within the body of the for loop.

  3. #3
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Why does your for loop start at 2? Going from 2 to 12 will only ask for input 11 times.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What am I missing?

    This mainly reflects the loop that was in my textbook, with the start point at 2. Didn't make any sense to me either, but someone in another forum said my problem was that the bottom S.o.p was inside my loop. After removing it from the loop, it worked. Thanks anyway guys!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What am I missing?

    I suggest your read problems with crossposting link below

    This thread has been cross posted here:

    http://www.techimo.com/forum/webmastering-programming/270454-java-se7-halp.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. missing something so simple....help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2011, 10:12 PM
  2. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  3. Missing drivers?
    By mjpam in forum JDBC & Databases
    Replies: 5
    Last Post: September 6th, 2010, 08:00 PM
  4. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  5. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM