• Post Reply Bookmark Topic Watch Topic
  • New Topic

Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>(); does not work  RSS feed

 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am getting this error:


Incorrect number of arguments for type Map<K,V>; it cannot be parameterized with arguments <Integer>
Syntax error on token ",", . expected
Incorrect number of arguments for type Hashtable<K,V>; it cannot be parameterized with arguments <Integer>
Syntax error on token ",", . expected
length cannot be resolved or is not a field
The type of the expression must be an array type but it resolved to List<Integer>

The code is:


///////////////////
The Map works this way:

[Added code tags - see UseCodeTags for details]

But it does not work when I add the user input code because of the ArrayList.

Thank you in advance!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yoland. Welcome to the Ranch!

Your problem seems to be this line (which the compiler error message would tell you):

You can't do multiple assignments on a line like that, and Map and Hashtable both take two type parameters, not one. Were you really meaning to try and create two variables and two objects there? What were you hoping to do?
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

For numbers summing to 35, I would like to display:

Found numbers : 20 and 15

Found numbers : 25 and 10

Found numbers : 30 and 5

when the user input is:

15,5,10,20,25,30

I still get the same error message changing the statement to:
Map<Integer>, ArrayList<Integer> intMap = new Hashtable<Integer>, ArrayList<Integer>();

Thank you!
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Should I post the question under a different subject? I don't see it in the list now.

Thank you!
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The:
List<Integer> userInputArray = new ArrayList<Integer>();

is not compatible with:

Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>();
and:
Map<Integer>, ArrayList<Integer> intMap = new Hashtable<Integer>, ArrayList<Integer>();

is not working either.

Thank you!
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want as the value of your map? A single Integer or a List<Integer>?
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dennis,


For numbers summing 35, I would like to display:

Found numbers : 20 and 15

Found numbers : 25 and 10

Found numbers : 30 and 5

out of user input:

15,5,10,20,25,30


Thank you!
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like both: the list of user input numbers = 15,5,10,20,25,30
and pairs that add to 35
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear with Map, HashMap, HashTable, ArrayList, List, etc
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yoland carls wrote:I'm not clear with Map, HashMap, HashTable, ArrayList, List, etc


I don't think the issue here is an understanding of collections. This...

yoland carls wrote:
Map<Integer>, ArrayList<Integer> intMap = new Hashtable<Integer>, ArrayList<Integer>();

is not working either.


Is not valid Java. We don't know what you are trying to do. Perhaps it would help us, if you told us what you think the line is supposed to do.

Henry
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Sorry for the confusion.

I just want this program:


import java.util.Arrays;
import java.util.Map;
import java.util.Hashtable;

class two {


private static int[] intArray = {15,5,10,20,25,30};
private static int sum = 35;


public static void main(String[] args) {
Arrays.sort(intArray);
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>();
for (int i=0; i<intArray.length; i++)
{
intMap.put(i, intArray[i]);
System.out.println("i " + i + " array " + +intArray[i]);

if(intMap.containsValue(sum - intArray[i]))
System.out.println("Found numbers : "+intArray[i] +" and "+(sum - intArray[i]));

}
System.out.println(intMap);
}

}

THAT GIVES THE BELOW RESULT:
Found numbers : 20 and 15
Found numbers : 25 and 10
Found numbers : 30 and 5
{5=30, 4=25, 3=20, 2=15, 1=10, 0=5}

WHICH IS FINE. I JUST WANT TO CHANGE THE " private static int[] intArray = {15,5,10,20,25,30}; "
TO USER INPUT. SO I CAN ENTER THE NUMBERS 20, 15, ETC. IN THE CONSOLE.

THIS IS NOT WORKING NOR OTHER THINGS I TRIED:

import java.util.Arrays;
import java.util.Map;
import java.util.Hashtable;
import java.util.Scanner;

class Four {


private static int[] intArray;
private static int sum = 35;


public static void main(String[] args) {
Arrays.sort(intArray);
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
for (int i=0; i<intArray.length; i++)
{
intMap.put(i, intArray[i]);
System.out.println("i " + i + " array " + +intArray[i]);

if(intMap.containsValue(sum - intArray[i]))
System.out.println("Found numbers : "+intArray[i] +" and "+(sum - intArray[i]));

}
System.out.println(intMap);
}

}



 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify more:

I would like to display the same result below but with user input:

THAT GIVES THE BELOW RESULT:
Found numbers : 20 and 15
Found numbers : 25 and 10
Found numbers : 30 and 5
{5=30, 4=25, 3=20, 2=15, 1=10, 0=5}

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:
I JUST WANT TO CHANGE THE " private static int[] intArray = {15,5,10,20,25,30}; "
TO USER INPUT. SO I CAN ENTER THE NUMBERS 20, 15, ETC. IN THE CONSOLE.

THIS IS NOT WORKING NOR OTHER THINGS I TRIED:




Well, you did start it...

* You removed the array initialization
* You created a scanner, which you don't use in the rest of the program
* And you asked the user to enter 10 numbers

... but ... you didn't finish it. The rest of the program is the same. It simply processes the (now uninitialized) array.

Henry
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for your help.

When I add the below code for the user input:

int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
intArray.add(x);
} else {
System.out
.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);


between: System.out.println("Enter 10 Numbers ");
and

for (int i=0; i<intArray.length; i++)

I get the message:

Cannot invoke add(int) on the array type int[];
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although your code is not legible, I went ahead and found this,



What do you think this line does?
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It adds an int to the array. Why it cannot add an int? What is the statement to compile without errors?

Thanks in advance!
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no add() method for an array. Array elements can be manipulated (and accessed) using the bracket notation after it has been initialized. So if you want an array you declare it by (you probably know all this),

type[] typeName; // declare
typeName = new type[10]; // create and initialize it
typeName[0] = x; // assign x to first element; arrays elements start with 0

Please read for a more clear explanation http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the change inArray[0] = x;

and I get the following error:

Enter 10 Numbers
10
Given Number is 10
Something went wrong :-(
Exception in thread "main" java.lang.NullPointerException
at Four.main(Four.java:41)
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you create the array object in addition to declaring it? Do you have any of the following statement in your program?

int[] anArray = new int[10]; // emphasis on the new operator

int[] otherArray = {1, 2}; // another way to create and initialize the array called shortcut notation

 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I tried that too and I am getting the error below:

Enter 10 Numbers
2
Given Number is 2
4
Given Number is 4
30
Given Number is 30
5
Given Number is 5
1
Given Number is 1
7
Given Number is 7
8
Given Number is 8
3
Given Number is 3
4
Given Number is 4
5
Given Number is 5
Exception in thread "main" java.lang.NullPointerException
at Four.main(Four.java:42)
------------------The is the code now ------------------------


import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Hashtable;
import java.util.Scanner;

class Four {


private static int[] intArray;
private static int sum = 35;


public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);

int[] intArray = new int[10];
intArray[0] = x;

} else {
System.out.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>();
for (int i=0; i<intArray.length; i++)
{
intMap.put(i, intArray[i]);
System.out.println("i " + i + " array " + +intArray[i]);

if(intMap.containsValue(sum - intArray[i]))
System.out.println("Found numbers : "+intArray[i] +" and "+(sum - intArray[i]));

}
System.out.println(intMap);

}
}
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to enter numbers and then for numbers summing 35, I would like to display:

Found numbers : 20 and 15

Found numbers : 25 and 10

Found numbers : 30 and 5

out of user input:

15,5,10,20,25,30

But I am getting the error mentioned above.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:I want to enter numbers and then for numbers summing 35, I would like to display:

Yoland,

We are now 20 posts into this thread, and I (and, I suspect, several others who have contributed to it) still don't understand what you are trying to do.

There is no point in repeating the same thing over and over again. The problem is: it's not explaining the problem to us.

So - question for you - WHY do you want to do this? Maybe that will help you work out what we need.
Alternatively: give us the problem question - verbatim - ie, exactly as it was given to you. You can even copy and paste it if you like.

Second point: Why are you trying something so complex, when you're clearly still having problems with basic Java syntax?
This is not a criticism, it's just a fact: you need to learn to walk before you can run.

Nothing I've seen so far suggests that you actually even need a Map, so perhaps that's where your problems are stemming from; but until we know WHAT you want to do (and why you want to do it), it's very difficult to advise anything.

Winston
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for the confusion. But I just want to enter numbers, then display the pairs that will add up to 35. So I have the code working for displaying the pairs. I just cannot figure out how to make the user input scanner work. The Map is to display the pairs that add to 35. I appreciate your patience. Thanks
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of what I want to display:

Numbers 10, 25, 3, 4,6, 30. 5, 7,1 are entered.

The program will display:

10 and 25
30 and 5

and the map: 10,25,3,4,6,30,5,7,1


I can get this result if I initialized the array.

But I cannot make it work with the scanner so I can enter different numbers every I run the program.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:I am sorry for the confusion. But I just want to enter numbers, then display the pairs that will add up to 35. So I have the code working for displaying the pairs. I just cannot figure out how to make the user input scanner work. The Map is to display the pairs that add to 35. I appreciate your patience. Thanks

OK. Now we have a working requirement. AND your problem. Now we can start helping.

Your problem is with Scanner, so forget about everything else and concentrate on that.

Write a program that simply reads in a number and prints it out; and don't write anything else until it works. ALWAYS. No matter what the user types in. If they type in an "X", what do you want it to do?

You might find the UserInput page worth reading, but I warn you: it's not short - because user input is NOT easy (and neither is Scanner).

Once you having a working program that accepts a single number, then come back and we can deal with the rest of your problem.

But your main issue at the moment is that you're taking on too much stuff.

HIH

Winston
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This is the program to enter the numbers with the scanner:

import java.util.*;


public class One {

public static void main(String[] args) {

List<Integer> userInputArray = new ArrayList<Integer>();

Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
userInputArray.add(x);
} else {
System.out
.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);


System.out.println("Numbers from USER : " + userInputArray);
Collections.sort(userInputArray);
System.out.println("Numbers from USER : " + userInputArray);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}

}
}

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:This is the program to enter the numbers with the scanner:

No. That program enters TEN numbers. Deal with problems one at a time. If you can't enter ONE number, you will never get it to work with ten.

A major part of good programming is problem isolation. And that also holds true for debugging.

Try again. Write a program that accepts ONE number and then prints it out (to make sure that what you entered is what you get); and test it with LOTS of different input.

Winston
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, when I add the code to display the pairs that add to 35 as below:
(The name of the array is inArray here)

import java.util.*;

class Four {


private static int[] intArray;
private static int sum = 35;


public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers ");
int count = 0;
int x;
try {
do {
x = input.nextInt();
if (x != 0) {
System.out.println("Given Number is " + x);
int[] intArray = new int[10];
intArray[0] = x;

} else {
System.out.println("Program will stop Getting input from USER");
break;
}
count++;
} while (x != 0 && count < 10);
} catch (InputMismatchException iex) {
System.out.println("Sorry You have entered a invalid input");
} catch (Exception e) {
System.out.println("Something went wrong :-( ");
}
// Code to display pairs that add to 35
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>();
for (int i=0; i<intArray.length; i++)
{
intMap.put(i, intArray[i]);
System.out.println("i " + i + " array " + +intArray[i]);

if(intMap.containsValue(sum - intArray[i]))
System.out.println("Found numbers : "+intArray[i] +" and "+(sum - intArray[i]));

}
System.out.println(intMap);

}
}

Running the program I get error: Exception in thread "main" java.lang.NullPointerException
at Four.main(Four.java:39)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:So, when I add the code to display the pairs that add to 35 as below:

Whoa, hoss. You're racing ahead. Does the program you wrote before even WORK? I can't even understand how you'd know, because your display statements are WRONG.

Like I said, forget about summing and solve your problem, which is INPUT. Write a simple program that accepts ONE number and prove it works.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised nobody has told you to read UseCodeTags yet.
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, here it is:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:Sorry, here it is:

And does it work? I don't see anything in that code that proves that it does (which is why I suggested displaying the value).

Once you've got that working, try and see if you can get ten integers...and so on.

You need to build up programs gradually; otherwise, you're in for a world of hurt.

Winston
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it displays:

Enter a Number
9
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yoland carls wrote:Yes, it displays:
Enter a Number
9

OK, so stick that code in a loop and make sure you can get 10 integers. And test that it works. Then, and ONLY then, will you be ready to tackle the rest of your assignment.

Winston
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to check what happens if you type 'X'
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Here is the code to enter ints and with a check for no ints:



Thank you!
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I made it...





Thank you for breaking up the steps!
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a while loop on line 12 which you always terminate after one iteration ?
Why have you removed the error checking ?
Why are you using a Map ? You only make use of the values in it, so a List would be a better choice.
 
yoland carls
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good points. I will try to make it better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!