0

i'm trying to find my very first steps in java and while developping the code below

import java.util.Scanner;

public class MoyEcart {

    public static void main(String[] args) {
        float moy= 0, ecart_type= 0, somme= 0, carre= 0, moy_tmp, part_one;
        Scanner sc= new Scanner(System.in);
        System.out.print("Dernier terme de la suite:");
        int n = sc.nextInt();

        float[] t= new float[n];
        for(int i=0; i<n; i++) {
            System.out.print("Terme " + i + ":");
            t[i] = sc.nextFloat();
        }

        for(int i=0; i<n; i++) {
            somme+= t[i];           
        }
        moy = somme/n;
        moy_tmp = moy * moy;

        for(int i=0;i<n;i++) {
            carre += t[i] * t[i];  
        }
        part_one = carre/n;
        ecart_type=(float) Math.sqrt(part_one - moy_tmp);
        System.out.println("Moyenne ="+moy);
        System.out.println("Ecart type="+ecart_type);
    }
}

I'm getting this error, and i couldn't find a solution to it, so please if you guys now a way out to this, i'll be grateful.

Exception in thread "main" java.util.InputMismatchException 
  at java.util.Scanner.throwFor(Scanner.java:864) 
  at java.util.Scanner.next(Scanner.java:1485) 
  at java.util.Scanner.nextFloat(Scanner.java:2345) 
  at MoyEcart.main(MoyEcart.java:17) 
6
  • You should consider formatting your code, for readability. And regarding your problem: Learning to debug is part of learning to program. Your next challenge is probably to understand the stack trace. I wrote a beginner's guide that might help. Commented Dec 26, 2014 at 22:20
  • Could you put an example of your input? Commented Dec 26, 2014 at 22:21
  • 2
    Can you provide the stack trace you are getting as well as what you are typing in the Console at the prompts? Commented Dec 26, 2014 at 22:24
  • "Input" means what you type on the console and the program reads and proceseses. Commented Dec 26, 2014 at 22:25
  • 1
    Let me hazard a guess here: you are using French locale but entering the number as "10.2", or using US locale, but entering the number as "10,2" (French notation). Commented Dec 26, 2014 at 22:31

1 Answer 1

1

I ran your code, and it works fine for me! You should first enter a single number and press enter. After that, you need to input that amount of numbers one at a time. This means you need to press enter after every number. The output I got from your program looks like this:

Dernier terme de la suite:3
Terme 0:1
Terme 1:2
Terme 2:3
Moyenne =2.0
Ecart type=0.8164965
Sign up to request clarification or add additional context in comments.

3 Comments

yes it does. The first number has to be an integer because you're calling nextInt(). But the others work as being a float.
It doesn't work with floats, i tried it. try to give numbers with comma, the exception will appear again
You can't use commas after the numbers. Try it again, but press enter after every number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.