InputMismatchException when reading float via Scanner.nextFloat() [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
When I use the program with an integer value it works, but when I input a float (such as 47.3) it doesn't. Can you please help me in defining the problem?
the code:
package ex1;
import java.util.Scanner;
public class test {
static Scanner input = new Scanner(System.in);
public static void main (String[] args) {
float n;
System.out.println("enter a number:");
n = input.nextFloat();
if (n > 0 && n % 2 == 0) {
System.out.println("the number is positive and even");
}
else if (n > 0 && n % 2 != 0) {
System.out.println("the number is positive and odd");
}
else if (n < 0 && n % 2 == 0) {
System.out.println("the number is negative and even");
}
else if (n < 0 && n % 2 != 0) {
System.out.println("the number is negative and odd");
}
else if (n == 0) {
System.out.println("the number is zero");
}
}
}
the problem:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
at ex1.test.main(test.java:14)
thanks

A comma is considered as a decimal separator in Java. So to use a Float input you should replace the '.' by ','

Related

How to remove the amounts of ifs from the application? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
The code I say has to complete these assignments:
Given an integer,N, perform the following conditional actions:
If N is odd, print Weird
If N is even and in the inclusive range of 2 to 5 , print Not Weird
If N is even and in the inclusive range of 6 to 20 , print Weird
If N is even and greater than 20 , print Not Weird
Complete the stub code provided in your editor to print whether or not N is weird.
My code looked like this:
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
int Numberparorimpa = N % 2;
if(N < 2 || Numberparorimpa ==1 || N <=20 && N >=6 ){
System.out.println("Weird");
}else{
if(N >=2 && Numberparorimpa == 0){
System.out.println("Not Weird");
}else{
if(Numberparorimpa == 0 && N >=6 || N<=20){
System.out.println("Weird");
}else{
if(Numberparorimpa== 0 && N> 20){
System.out.println("Not Weird");
}else{
return;
}
}
}
}
}
}
How can I reduce the IFs of this code?
I think an optimize version could be this :
if (N % 2 == 1 || (N >= 6 && N <= 20)) {
System.out.println("Weird");
}
else {
System.out.println("Not Weird");
}
If N is odd or N in range of 6 to 20 it's weird.
Else N is either even or not in the range so it's not weird.
You can reduce the complexity and improve the readability of your code by extracting your logic to method/class etc. Also, nested conditions are hard to read, you should avoid it.
Example:
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
String print = isGivenNumberWeird(n) ? "Weird" : "Not Weird"
System.out.println(print);
}
private static boolean isGivenNumberWeird(int n) {
boolean isOdd = n % 2 == 1;
if (isOdd) {
return true;
}
if (n >= 2 && n <=5) {
return false;
}
if (n >= 6 && n <=20) {
return true;
}
if (n > 20) {
return false;
}
}

algorithm is not working with some inputs [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm new to programming, and I saw a problem in a website to practice. I was asked to create an algorithm that takes a positive integer, let's say n. If n is even the algorithm divides n by 2. If n is odd, it multiplies n by 3 and adds 1. This is repeated until it gets n = 1. For example, n=3 the the output would be 3 > 10 > 5 > 16 > 8 > 4 > 2 > 1
My code looks like this:
import java.util.*;
public class practice {
public static void solve(int n){
while(n > 1){
System.out.print(n + " ");
if(n % 2 == 0){
n=n/2;
}else{
n=(3*n)+1;
}
}
System.out.print(n);
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int input = keyboard.nextInt();
solve(input);
}
}
(Edit: Last output is suppose to be 1, but instead I'm getting random negative numbers with the next inputs):
138367
270271
665215
704511
I have no idea what I'm doing wrong...
I think int is going out or range or out of bytes after some calculation try the given code using long:
import java.util.*;
public class practice {
public static void solve(long n){
while(n > 1){
System.out.print(n + " ");
if(n % 2 == 0){
n=n/2;
}else{
n=(3*n)+1;
}
}
System.out.print(n);
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
long input = keyboard.nextLong();
solve(input);
}
}

Java If-Else in HackerRank, Is my code good or can I get better solution for the problem [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
Problem Statement
Task
Given an integer,n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
Complete the stub code provided in your editor to print whether or not n is weird.
Input Format
A single line containing a positive integer,n.
Constraints
1<=n<=100
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
Explanation
Sample Case 0: n=3
n is odd and odd numbers are weird, so we print Weird.
Sample Case 1: n=24
n>20 and n is even, so it isn't weird. Thus, we print Not Weird.
My solution to the problem.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2==0){
if(N>1){
if(N<6){
System.out.println("Not Weird");
}
else if(N<21){
System.out.println("Weird");
}
else{
System.out.println("Not Weird");
}
}
}
else{
System.out.println("Weird");
}
scanner.close();
}
}
Can I get suggestions regarding the solution. Thank You.
The conditions should be merged: the result is Weird for any number in the range 6 <= n <= 20 OR n is odd, otherwise it is Not Weird:
System.out.println((6 <= n && n <= 20 || n % 2 != 0) ? "Weird" : "Not Weird");
// or
System.out.println((5 < n && n < 21 || n % 2 != 0) ? "Weird" : "Not Weird");
if (~N % 2 == 0 || N <= 20 && N >= 6) {
System.out.println("Weird");
} else if (N % 2 == 0 && N > 20 || (N >= 2 && 5 <= N)) {
System.out.println("not Weird");
}

Multiples of 3 print hippity and on multiples of 4 print hop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
//Name of Program: HippityHop.java
//Entered by: No Name
//Date: 10/19/2020
import java.util.Scanner; // Needed for the Scanner class
public class HippityHop
{
public static void main(String[] args)
{
// get scanner to read input
Scanner keyboard = new Scanner(System.in);
for(int x=1; x <= 100; x++){
if(x % 3 && x % 4) {
System.out.println(x);
}else{
if(x % 3 == 0){
System.out.println("Hippity");
}
if(x % 4 == 0){
System.out.println("Hop");
}
}
}
}
}
I am trying to create a program that on multiples of 3 it prints "Hippity" and on multiples of 4 it prints "hop". I seem to be getting a bad operand error. What can I do to fix it?
The following expression:
if(x % 3 && x % 4) {
Isn't a proper one. What x%3 is doing is calculating the modulus. You never compared it to anything, so it's throwing a bad operand error. That's like saying in real life:
if x modulus 3 then do this
Or, just for the sake of the argument (and to make it easier to understand), it's like saying:
if x subtract 3 then do this
Instead, it should be if(x%3!=0 && x%4!=0), like so:
import java.util.Scanner; // Needed for the Scanner class
public class HippityHop
{
public static void main(String[] args)
{
// get scanner to read input
Scanner keyboard = new Scanner(System.in);
for(int x=1; x <= 100; x++){
if(x % 3 !=0 && x % 4 !=0) {
System.out.println(x);
}else{
if(x % 3 == 0){
System.out.println("Hippity");
}
if(x % 4 == 0){
System.out.println("Hop");
}
}
}
} }

What is my mistake in this problem. Simple Comparison Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Task
Given an integer, n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5 , print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
I have written a code but it is showing error in printing 18 and 20.
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.close();
if(N%2 != 0){
System.out.println("Weird");
}
else if(N%2 ==0 && N>=2||N<=5)
{
System.out.println("Not Weird");
}
else if(N%2 ==0 && N>=6||N<=20)
{
System.out.println("Weird");
}
else if(N%2 ==0 && N>20)
{
System.out.println("Not Weird");
}
}
}
You need to change your or condition || to and condition && to check for the range like
else if(N%2 == 0 && N >= 2 && N <= 5)

Categories