#Calculating The amount of rain over time

1 messages · Page 1 of 1 (latest)

wooden pilot
#

So, first, i'm not able to get the program to accept output for each iteration. Can anyone help? My code is below.

import java.util.Scanner;
import java.text.DecimalFormat;

public class RainFall {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int Years, Months; 
    double Rain, totalRain = 0.0, averageRain;
    
    Scanner scan = new Scanner(System.in);
    
    DecimalFormat fmt = new DecimalFormat("0.##"); 
    
    
    System.out.print("For how many years did it rain? ");
    Years = scan.nextInt();
    
    while (Years < 1) {
        System.out.print("Enter data for at least one year, for how many years did it rain? ");
        Years = scan.nextInt();
    }
    
    System.out.print("For how many months did it rain? ");
    Months = scan.nextInt();
    
    System.out.println();
    
    for (int Y = 1; Y <= Years; Y++) 
        System.out.print("Enter the amount of rainfall for year " + Y + ": ");
        Rain = scan.nextDouble();
        
    
    
        for (int M = 1; M <= Months; M++) 
            System.out.println("Enter the amount of rainfall for month " +  M);
            Rain = scan.nextDouble();
        
        
    while (Rain < 0.0) {
        System.out.print("Negative rainfall invalid! Enter a positive number: ");
        Rain = scan.nextDouble();    
    }
    
}

}

summer anchorBOT
#

<@&987246399047479336> please have a look, thanks.

#

You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.

#

An error has occurred while trying to communicate with ChatGPT.
Please try again later.

summer anchorBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

hazy zephyr
#

You're never changing your totalRain nor averageRain value, you're missing some { so your for year and for months block end after the first line below it. Normally your IDE should advise you of this.

#

Also please use:

summer anchorBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.