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.
Related
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 1 year ago.
Improve this question
import java.util.*
public class text_baes_adventure_game {
public static void main(String[] args){
ArrayList<String> list1 = new ArrayList<String>()
list1.add("Yes")
Scanner input = new Scanner(System.in)
String name = ""
String y_N1 = ""
System.out.println("Hi what is your name: ")
name = input.nextLine()
System.out.println("Hi "+name+" whould you like to play a dungeon game?: ")
y_N1 = input.nextLine()
if (y_N1 == list1){
System.out.println("Ok lets start!")
}
else{
System.out.println("Ok bye!")
}
}
}
1.How do I add a list to the if statement
2.I'm a beginner in java so can someone help me
if (y_N1 == list1){
This doesn't check whether or not the list contains the String y_N1, it checks whether both objects point to the same Object in memory, which isn't possible.
If you mean to check whether or not the list contains the String, use this:
if ( list1.contains(y_N1))
instead.
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
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 5 years ago.
Improve this question
I can see that this topic has been heavily discussed, however, I was not able to find an answer to my problem within previous discussions. That being said, I have a very simple problem where I want to ask a user to input a list of cities. After being entered, I am storing the list in an ArrayList cities and using collections.sort to sort them. For some reason, collections.sort is not sorting my ArrayList. Example: User input is "Atlanta, Washington DC, New York". My output, when running the program, is unsorted.
public class CitySortDemo {
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("enter as many cities as you can!");
cities.add(input.nextLine());
Collections.sort(cities);
for (int i = 0; i < cities.size(); i++){
System.out.println(cities.get(i));
}
}
}
Your code adds a single string to the collection, "Atlanta, Washington DC, New York". A collection with only one entry is unaffected by sorting. :-)
You probably meant to break that string up, perhaps by splitting it on a comma:
cities.addAll(Arrays.asList(input.nextLine().split("\\s*,\\s*")));
Live Example
That splits the one string into an array of them on a comma optionally preceded and/or followed by whitespace, and adds them all to the collection.
Either you can ask the user how many cities are expected to sort or specify a character that when it is seen, stop taking input and sort them. In this your code, it just takes one line as a string. For example, it takes cities until the user enters the specifier character in which the code is ! then sort.
import java.util.*;
class CitySortDemo {
public static void main(String[] args) {
final String specifier = "!";
String str;
ArrayList<String> cities = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("enter as many cities as you can!");
str = input.nextLine();
while (! str.equals(specifier)) {
cities.add(str);
str = input.nextLine();
}
Collections.sort(cities);
cities.forEach(System.out::println);
}
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 5 years ago.
Improve this question
The purpose of my code is to enter a pin, and it'll check if it's right or not. If it isn't, the question will loop.
For some reason, my code doesn't loop properly, and a lot of the code is underlined. Specifically the while loop itself and the second JOptionPane
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
while(correctPin != sMaybePin); <--
Look at that ; that terminates the loop right there. You need to remove that.
You never update sMabyPin which is the variable you are checking against. If you do what #John and #ANS suggested you'll be stuck in an infinite loop.
Remove the ; after the while statement and correct set the value of the variable sMaybePin to the input vlaue and ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
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 6 years ago.
Improve this question
Example code:
import java.util.Scanner;
public class Split {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a few words: ");
String wordsWhole = scan.next();
String[] wordsSplit = new String[4];
wordsSplit = wordsWhole.split("//s+");
System.out.println("Second word: " + wordsSplit[1]);
}
}
The output:
Enter a few words: Why no work
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at test.Split.main(Split.java:12)
My String isn't splitting into the array like I would expect it to. Any ideas on why this is?
Line 12:
System.out.println("Second word: " + wordsSplit[1]);
There are several problems:
Scanner.next() will only return the first word (space-separated) in the input, use Scanner.nextLine() to get the entire line.
I'm guessing you're trying to split by spaces. If so, you should use backslashes rather than forward slashes in your regex ("\\s+").
You don't need to allocate the array before assigning it to the result of the split. Just use String[] wordsSplit = wordsWhole.split("\\s+");