Java Hashmap -- Key Value Pairs from Stdin to HashMap [closed] - java

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 5 years ago.
Improve this question
Java Hashmap -- Is it possible to read key value pairs from standard input directly into a hash map structure?
Say user types in
4 3
1 1
3 2
2 2
4 3
My idea is to do some sort of loop with repeated put.

Not really sure how you want to add them, as int's or Strings, but remember they have to be Objects.
class Inp {
public void scan() {
Scanner sc = new Scanner(System.in);
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
while (true) {
String sr = sc.nextLine();
String[] tk = sr.split(" ");
Integer key = 0, value = 0;
try {
key = Integer.parseInt(tk[0]);
value = Integer.parseInt(tk[1]);
}
catch (NumberFormatException e) {
break;
}
map.put(key,value);
}
}
public static void main(String[] args) {
Inp inp = new Inp();
inp.scan();
}
}
VERRRRYYYY long winded but you get the idea. Put in anything besides an integer to break out of the loop. Didn't know what you wanted.

Related

overwriting an old String input

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 hours ago.
Improve this question
I'm trying to make my program work with a new input instead of the old one when the user clicks an option from the options menu
I don't what the problem that is not causing the program to continue with the new input instead of the old one. thanks in advance.
public static String optionSix (String input)
{
String newInput;
Scanner scnr = new Scanner (System.in);
System.out.println("Enter List of Words Seperated by Spaces: ");
newInput = scnr.nextLine();
input = newInput;
return input;
}
Java is pass-by-value, so you can't override a string's value like that. You'll have to assign the new value to it:
public static void main(String args[]) {
String someOldString = "some old value";
someOldString = optionSix(); // No need to pass the original value either
}

Stopping while loop [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 2 years ago.
Improve this question
can anybody help me to stopp this infity loop?
This loop shoud stopp if an input is a String.
All digits should be added to a list.
Thank you!
public static void main(String[] args) {
addDigitsToList();
}
public static void addDigitsToList() {
ArrayList<Integer> list = new ArrayList<Integer>();
int input = 0;
while (true) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
input = Integer.parseInt(reader.readLine());
list.add(input);
} catch (NumberFormatException | IOException e) {
for (Integer integer : list) {
System.out.println(integer);
}
}
}
}
If you want to stop the infinity loop than use the keyword
break;
on which condition you want to stop it. You told us to stop when the input is a string than you should update your code-
Right after the for loop add this :
break;

How can I input only integer and this integer will be only ending with zero? [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 8 years ago.
Improve this question
I am trying to validate input values to pass only integers that are divisible by 10. The code below is failing.
public static void main(String args[]) {
Scanner scan =new Scanner(System.in);
ArrayList<Integer> liste = new ArrayList<Integer>(); // I have filled my array with integers
int x=scan.nextInt();
int y=x%10;
do{
if(y==0){
liste.add(x);}
else if(y!=0){
System.out.println("It is not valid"); continue;
}
else
{System.out.println("Enter only integer"); continue;
}
}while(scan.hasNextInt()); }
System.out.println(liste);
System.out.println("Your largest value of your arraylist is: "+max(liste));
You're calling scan.nextInt() twice. Each time you call it, it will read another int from the input. Thus, if your input was something like
10
5
13
then the 10 would pass the scan.nextInt()%10==0 check, and then 5 would be added to the list. Store the result of scan.nextInt() in a variable first, so the value won't change.
Instead of
if(scan.nextInt()%10==0){
liste.add(scan.nextInt());}
do
int num = scan.nextInt();
if(num%10 == 0){
liste.add(num);
}

How do I add white spaces in Java Array? [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 8 years ago.
Improve this question
I'm kind of new to this stuff, but what i want to do is just print out 00s from an int array that i created. I used a stringBuilder() to get rid of the commas and stuff. Now when I print out the numbers, they must have a space after every third 0 (a total of 11 0s). How do I do that? I only get a space after every 0 :-(.
here is what I got so far.
public class AccountNumber {
private int[] digits = new int [11];
// Methods Returns a string representation
public String toString() {
StringBuilder builder = new StringBuilder();
for (int value :digits) {
builder.append(value + " ");
}
String text = builder.toString();
return text;
//return Arrays.toString(digits);
}
public AccountNumber ( boolean random ){
}
}
The output I want is 000 000 000 00
I have another (main) class which creates the object for me. That's where the printing should happen.
public class Test1 {
public static void main (String [] args) {
//Random rand = new Random(false);
AccountNumber acc = new AccountNumber(false);
System.out.println(acc.toString());
//AccountNumber.AccountNumber();
}
}
Thank you
You are trying to pretty print 11-digit account number on your Account class. You want to insert space after each 3 digits. If my assumptions are correct, you just need a counter to see which digit you're on and test if that digit can be divided by three:
int index= 0;
for (int value :digits) {
builder.append(value);
if (index %3 == 0) {
builder.append(" ");
}
++index;
}
This could be written in more clear way by using classic for loop, but I don't know which type your digits field is.

Displaying the First 100 Words Of a Program that Contains a File - Java [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 8 years ago.
Improve this question
I have a program that reads a file in order to sort the file alphabetically. So the output of the program is displayed in an ascending order (From A-Z if applies). However, I want my program to just output the first 100 words by ignoring the rest of it. Is there a Unix command that allows me to carry out this function? or do I have to implement a code/algorithm within my program in order to accomplish it?
It should be something like:
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int numberword=100;
int count=0;
while (sc2.hasNextLine() && count<numberword) {
Scanner s2 = new Scanner(sc2.nextLine());
boolean b;
while (b = s2.hasNext() && count<numberword) {
String s = s2.next();
count++;
System.out.println(s);
}
}

Categories