Cant print the value of scan.nextIn(); [closed] - java

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 4 years ago.
Improve this question
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);

In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input

Related

Why I can't add elements to HashMap (Java) [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
I don't know what I'm doing wrong... I have the next code:
public class Administrator {
private static Map<Integer, Professor> professors = new HashMap<Integer, Professor>();
private static void addProfessor(){
Scanner scanner = new Scanner(System.in);
System.out.println("\nADD PROFESSOR");
System.out.print("\tId: ");
Integer id = scanner.nextInt();
System.out.print("\tName: ");
String name = scanner.next();
System.out.print("\tLast name: ");
String lastname = scanner.next();
System.out.print("\tInit date (AAAA/MM/DD) (including slashes): ");
LocalDate date = LocalDate.parse(scanner.next());
if(professors.put(id, new Professor(id, name, lastname, date)) != null) {
System.out.println("Professor added successfully.");
} else {
System.err.println("We can't add the professor.");
}
}
}
And when I call the addProfessor method, the error "We can't add the professor." is printed. I don't know why the element is not added to the HashMap. There isn't any exception on my console, so I don't have any way to know whats wrong.
When calling Map.put(), the previous entry for the given key in the map is returned. If put() returns null, then the entry is added, but it didn't overwrite anything.

Java scanners - skip all "non-int" and "less than 0" inputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to loop through and skip all inputs that are less or equal 0 or non integers.
I have written this code, but it doesn't work and I don't understand why.
while(!userInput.hasNextInt() || userInput.nextInt() <= 0) {
userInput.next();
}
return userInput.nextInt();
I think you need to change a logic a bit, for example:
while (userInput.hasNext()) {
if (userInput.hasNextInt()) {
int intValue = userInput.nextInt();
if (intValue > 0) {
return intValue;
}
}
userInput.next();
}
Because when you're trying to verify that int value is less or equal zero userInput.nextInt() <= 0, you're actually getting the value.
So, if it's not true, you will go to this line return userInput.nextInt();, but cursor will already be on the next value.
You can check whether there is any user input or not. If there is take the input and then process it else continue with the loop.
while(userInput.hasNextInt()){
int a=userInput.nextInt();
if(a>=0){
return a;
}else
continue;
}

Why isn't this recursion working in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I am trying to calculate the factorial of the integer user inputs, but it does not return anything. Why?
Thanks.
import java.util.Scanner;
`class App {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
int recurse = factorial(n - 1);
int result = n * recurse;
return result;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to calculate its factorial: ");
int users = input.nextInt();
factorial(users);
}
}
The problem in your code is that you are have not giving a print statement for displaying factorial of the number entered. Just returning a value will not print it. If you are working in BlueJ environment, you can only use the code by directly executing the method factorial. Thank You.
The problem in your code is that , you are have not given a print statement for >displaying factorial of the number entered.

How do I loop a statement til condition is met ?Java [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 6 years ago.
Improve this question
Scanner scanner = new Scanner(System.in);
System.out.println("1. Do you like computers?");
String input = scanner.nextLine();
if (input.equals("no")) {
System.out.println("You should!");
if (input.equals("yes")){
System.out.println("I like computers too!");
If the user inputs anything else than "yes" or "no", how would i repeat the question til the user inputs "yes" or "no". I am new to java so detailed explanations will help.
Going off of the code nhouser9 provided (which is case-sensitive), one way to check for all variants ("Yes", "YES", "yes") is to use the .toUpperCase() method.
String input = "";
while (!input.toUpperCase().equals("YES") && !input.toUpperCase().equals("NO")) {
input = scanner.nextLine();
}
String input = "";
while (!input.equals("Yes") && !input.equals("No")) {
input = scanner.nextLine();
}
The above should loop until the user has entered a valid input. Note that the input here is case sensitive - you can add more ifs to check for other cases if you need to.

If statement not running: any ideas? [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 8 years ago.
Improve this question
I am trying to compare the third number to the last number and the fourth number to the fifth number. The if statement isn't running at all any help would be appreciated.
import java.util.*;
class Problem2{
public static void main (String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number between 100000 and 999996: ");
int m = s.nextInt();
if(m < 100000 || m > 999996){
System.out.println("Out of range!");
}
else{
String j = Integer.toString(m);
for(int i=2;i<6,i++){
System.out.println(j.charAt(i));
if(j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2)){
System.out.println("Works!");
}
}
}
}
}
You are incrementing the value, not the index of the character.
Change this:
if (j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2))
To this:
if (j.charAt(i) == j.charAt(i+3) && j.charAt(i+1) == j.charAt(i+2))
// increment indexes --^ --^ --^
Move the additions inside of the charAt() calls.
if(j.charAt(i) == (j.charAt(i+3))&& (j.charAt(i+1)) == (j.charAt(i+2)))

Categories