I would like to ask why I am getting an InputMismathException?
I have declared a variable of type double and when I assign it a point value e.g.(4.6) it throws me:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at Exercises.ComputingMeanAndStandartDeviation_5_21.main(ComputingMeanAndStandartDeviation_5_21.java:18)
Here is the code:
package Exercises;
import java.util.*;
public class ComputingMeanAndStandartDeviation_5_21
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double sum = 0;
double number = 1;
double counter = 1;
System.out.println("Enter ten numbers: ");
while(counter<10)
{
number = input.nextDouble();
sum +=number;
counter ++;
}
System.out.println(sum + " " + number + " " + counter);
double mean = sum / counter;
System.out.println("The mean is: " + mean);
}
}
Problem in locale
Locale.setDefault(Locale.US);
Scanner input = new Scanner(System.in);
US decimal delimiter "."(78.12) and not ","(78,12)
Related
import java.util.Scanner;
import java.text.DecimalFormat;
public class FutureValues {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Enter the present value: ");
int value = input.nextInt();
System.out.println("Enter annual interest rate: ");
double rate = input.nextDouble();
double newRate = rate / 1200;
System.out.println("Enter the number of months: ");
int months = input.nextInt();
int i;
for (i = 1; i <= months; i++) {
double newVal = value + (value * newRate);
System.out.println("The future value after " + i + " month is " + newVal);
}
}
}
I'm trying to get this to program to update the newVal to the next monthly deposit but it won't work for anything I try.
eg. "The future value after 1 month is 1004.79"
"The future value after 2 months is 1009.61"
and so on and so forth. I just cannot get it to update to the next value.
The Calculation of newVal should be done within the loop.
import java.util.Scanner;
import java.text.DecimalFormat;
public class FutureValues {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Enter the present value: ");
int value = input.nextInt();
System.out.println("Enter annual interest rate: ");
double rate = input.nextDouble();
double newRate = rate / 1200;
System.out.println("Enter the number of months: ");
int months = input.nextInt();
for (int i = 1; i <= months; i++) {
value = value + (value * newRate);
System.out.println("The future value after " + i + " month is " + value);
}
}
}
I have a file named Numbers.txt with the following content:
8.5
83.45, 90.2
120.00, 11.05
190.00
I have written code that uses the contents of the file to compute the sum and average of the numbers in the file however when I run the code, for the average the result is "NaN"
CODE:
package lab13;
import java.util.Scanner;
import java.io.*;
public class problem1 {
public static void main(String[] args) throws IOException
{
double sum = 0;
int count = 0;
double num,total = 0;
double average = total/count;
File file = new File("Numbers.txt");
Scanner scan = new Scanner(file);
while (scan.hasNext())
{
double number = scan.nextDouble();
sum = sum + number;
}
while (scan.hasNextDouble())
{
num = scan.nextDouble();
System.out.println(num);
count++;
total += num;
}
scan.close();
System.out.println("The sum of the numbers in " +
"Numbers.txt is " + sum );
System.out.println("The average of the numbers in " +
"Numbers.txt is " + average );
}
}
Output:
The sum of the numbers in Numbers.txt is 503.2
The average of the numbers in Numbers.txt is NaN
You need to do
double average = total/count;
after you have the values for total and count
But also note that
when while (scan.hasNext()) stream is exhausted then while (scan.hasNextDouble()) will also be exhausted
This can be overcome but just looping once
I'm writing a program where at the end I have to display the numbers I entered and the maximum and minimum of those entered numbers. However I'm running into a little problem, here is my code,
import java.util.*;
public class question3controlstructures {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
int numberEntered;
int numbersinput = 0;
String answer ="";
double sum = 0;
do {
System.out.println("Enter a number");
numberEntered = in.nextInt();
numbersinput ++;
System.out.println("do you want to enter another number?");
answer = in.next();
sum = sum + numberEntered;
} while (answer.equals("yes"));
System.out.println("The sum is: " + sum);
System.out.println("The average is: " + sum/numbersinput);
System.out.println(numberEntered);
}
}
The above comment are absolutely useful. However, here is little code
package com.mars;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Question3controlstructures {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.print("Enter integers please ");
System.out.println("(EOF or non-integer to terminate): ");
while (scan.hasNextInt()) {
list.add(scan.nextInt());
}
Integer[] nums = list.toArray(new Integer[0]);
int sum = 0;
int i = 0;
for ( ; i < nums.length; i++) {
sum = sum + nums[i];
}
System.out.println("sum..." + sum);
System.out.println("The average is: " + sum / i);
System.out.println(i);
System.out.println("max.. "+Collections.max(list));
System.out.println("min.. "+Collections.min(list));
scan.close();
}
}
As suggent in comments , you need a list to store the multiple numbered entered.
just compare the min and max every time you enter a number
int min = Integer.MAX_VALUE
int max = Integer.MIN_VALUE
int numberEntered;
int numbersinput = 0;
String answer ="";
double sum = 0;
do {
System.out.println("Enter a number");
numberEntered = in.nextInt();
System.out.println("YOU HAVE ENTERED: " + numbersEntered);
if (min > numberEntered) min = numberEntered;
if (max < numberEntered) max = numberEntered;
numbersinput ++;
sum = sum + numberEntered;
System.out.println("do you want to enter another number?");
answer = in.next();
} while (answer.equals("yes"));
System.out.println("The sum is: " + sum);
System.out.println("The average is: " + sum/numbersinput);
System.out.println(numberEntered);
//you can print your min max here.
The IntSummaryStatistics class together with Java 8's Stream API may be less verbose than dealing with min, max, sum and avg calculation manually.
public static void main(String[] args) {
// Get user input.
List<Integer> numbers = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
// No user friendly way to gather user input, improve!
numbers.add(scanner.nextInt());
}
// Transform input to statistics.
IntSummaryStatistics stats = numbers.stream()
.collect(Collectors.summarizingInt(Integer.intValue()));
// Print statistics.
String jointNumbers = numbers.stream()
.collect(Collectors.joining(", "));
System.out.printf("You entered %d numbers: %s\n, stats.getCount(), jointNumbers);
System.out.println("Min: " + stats.getMin());
System.out.println("Max: " + stats.getMax());
System.out.println("Sum: " + stats.getMax());
System.out.println("Avg: " + stats.getAverage());
}
I typed up this java program calculating simple interest in Eclipse.
import java.util.Scanner;
public class PercentageCalculator
{
public static void main(String[] args)
{
double x = 0;
double y = 0;
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter the value of x: ");
x = scanner.nextDouble();
System.out.println("Enter the value of y: ");
y = scanner.nextDouble();
System.out.println();
System.out.println("Calculating percentage: (x % of y): ");
System.out.println(x + " % of" + y + "is " + result);
System.out.println();
}
}
But when I tried to run it, it gave me this message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
result cannot be resolved to a variable
at PercentageCalculator.main(PercentageCalculator.java:19)
What does this mean and how can I fix it?
Declare you result variable
double result = 0;
then calculate the percentage and assign it to your result variable.
result = your calculation;
then print it
System.out.println(x + " % of" + y + "is " + result);
Hope this will help :)
You have not declare the result variable
double result=0;
I've been having trouble extracting the data from a text file and using it. I've got an assignment that requires me to get 10 doubles from the file and find the min, max, and average of the numbers. This is what I've got so far.
import java.util.*;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class DataAnalysis
{
static double i;
public static void main(String args[])
{
double sum =0;
Scanner inputFile = new Scanner("input.txt");
double min = inputFile.nextDouble();
double max = inputFile.nextDouble();
for(i = inputFile.nextDouble(); i < 10; i++)
{
if(i < min)
{
min = i;
}
else
{
if(i > max)
{
max = i;
}
}
}
double average = sum/ 10;
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + min);
System.out.println("Average: " + average);
}
}
It compiles just fine, but I get a Scanner InputMismatchException
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.nextDouble(Scanner.java:2413)
at DataAnalysis.main(DataAnalysis.java:20)
Any help with this would be appreciated!
It might be locale dependent. Decimal numbers are e.g written as 0,5 in Sweden.
Change your code so that it says e.g.:
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);