yoland carls

Greenhorn
+ Follow
since Oct 27, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by yoland carls

Hi, I made it...





Thank you for breaking up the steps!
11 years ago
Hello,

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



Thank you!
11 years ago
Sorry, here it is:

11 years ago
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)
11 years ago
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 :-( ");
}

}
}

11 years ago
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.

11 years ago
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
11 years ago
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
11 years ago
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);

}
}
11 years ago
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)
11 years ago
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!
11 years ago
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[];
11 years ago
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}

11 years ago