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 6 years ago.
Improve this question
How do I make sure that some how it doesn't throw exception at first time while checking condition with 1 and allow to check all the condition and then throws the exception related to first Condition that "Number is already added".
class Hello
{
List<Integer> newExample=new Arraylist<Integer>();
List<Integer> example=new Arraylist<Integer>();
example.add(1);
example.add(2);
example.add(3);
public void addExample()
{
for(Integer ex: example)
{
pl.adding(ex);
}
}
public void adding(Integer example)
{
if(example==1)//some work around so that rest of list of example (2,3) get chance to execute.
{
throw new NumberAlreadyAddedException("Number is already added")
}
newExample.add(example);
}
}
I believe in your case you should be checking for duplicate input at the point in time where you are receiving/adding the values to the list. I have slightly modified your code to do the check for a duplicate at the point where you are about to add the value to the List and if the value is found to be a duplicate, thrown an error immediately:
import java.util.*;
class Hello{
List<Example> exam = new ArrayList<Example>();
public void execute() throws Exception{
add(1);
add(2);
add(3);
add(1);
for(Example ex:exam){
test(ex)
}
}
public void add(Integer value) throws IllegalArgumentException{
if(exam.contains(value)){
throw new IllegalArgumentException("Number is already added.");
}
exam.add(value);
}
public void test(Example example){
System.out.println("Running test on Example: " + example);//pl.test(ex);
}
public static void main(String[] args){
Hello hello = new Hello();
hello.add(1);
hello.add(2);
hello.add(3);
hello.add(1);
}
}
class Example{}
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 2 years ago.
Improve this question
I'm trying to pass a simple array into a constructor of a class and kept getting a "cannot convert double[] to int" error. I'm really lost as to why.
import java.util.*;
public class Demo {
public static void main(String[] args) {
double[] data = {1,2,3,4,5,6,7,8,9,10,11,12};
Rainfall[] rain = new Rainfall[data];
}
}
import java.util.*;
public class Rainfall {
private double[] monthlyRain;
public Rainfall(double [] array) {
monthlyRain = array;
}
}
My IDE kept showing a red squiggly line underneath the "data" in
Rainfall[] rain = new Rainfall[data];
in the main method.
Use this instead...you are invoking constructor wrongly... with Rainfall[] rain = new Rainfall[data];
public class Demo {
public static void main(String[] args) {
double[] data = {1,2,3,4,5,6,7,8,9,10,11,12};
// Rainfall[] rain = new Rainfall[data]; wrong...
Rainfall[] rain = new Rainfall[]{new Rainfall(data) }; // correct
}
}
The thing is... Rainfal[] only holds an array of Rainfall objects...not one..
So thats..the correct way...sorry for error...
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
public class Test {
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.test();
System.out.println(t2.getNum());
}
}
public class Test2 {
private int num;
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
public int getNum() {
return num;
}
public void setNum(int bruh) {
this.num = bruh;
}
}
Whenever I create an object of Test2 and set the value of num using t2.test(), It prints out on the console as 0 when using t2.getNum(). Why does it return a value of 0? And how do I fix it so it actually gets the value I want? Class Test and Test2 are separate files as a side note.
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
You create a new instance of Test2 locally in the method, which has no impact on this instance.
public void test() {
setNum(3);
}
would be enough.
t2 and t or 2 different objects.
When you call t2.test(), you create a new object t and you set the attribute num of the t object, not t2. So when you call t2.getNum(), the num attribute of t2 keep is default value which is zero for integer in Java.
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 5 years ago.
Improve this question
class A{
public A(int i) {
System.out.println(1);
}
public A() {
this(10);
System.out.println(2);
}
void A() {
A(10);
System.out.println(3);
}
void A(int i) {
System.out.println(4);
}
}
public class MainClass{
public static void main(String[] args) {
new A().A();
}
}
I don't understand the output of this code. I am a beginner and I really want to know why is this happening, the execution track or what happens when this code runs?
You create new instance of A class with empty constructor. new A()
In empty constructor, first call is the constructor with the value this(10).
Int the constructor with int value you display "1". System.out.println(1)
Then it returns back to empty constructor and call "2". System.out.println(2)
Next, you call the method with no parameters in it. .A()
And it call the the method with parameters. A(10)
Then it prints "4". System.out.println(4)
Also returns back to previous method and print "3" as last operation. System.out.println(3)
https://i.imgur.com/i5l8kTA.png
Sorry for my English.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am attempting to use an arraylist inside of a hashmap to create and iterate counters for different characters fed to the processLine method. I think I have declared all of the variables and the catch statements should handle the cases in which there isn't an entry in the hashmap, but I am still getting a NullPointerException on the curCounts.set(i, 1); line in the second catch statement. I've probably made some dumb mistake, but I can't figure out what it is.
HashMap<Character, ArrayList<Integer>> charCounts;
public DigitCount() { charCounts = new HashMap<>(); }
public void processLine (String curLine) {
int length = curLine.length();
char curChar;
ArrayList<Integer> curCounts;
Integer curCount;
for(int i = 0; i < length; i++){
curChar = curLine.charAt(i);
try {
curCounts = charCounts.get(i);
} catch (NullPointerException ex) {
curCounts = new ArrayList<>();
}
try {
curCount = curCounts.get(i);
curCount++;
curCounts.set(i, curCount);
} catch (NullPointerException ex) {
curCounts.set(i, 1);
}
charCounts.put(curChar, curCounts);
}
linesProcessed++;
System.out.println("---------------------------" + linesProcessed);
}
Edit: Yes, I did call DigitCount.
public static void main(String args[]) throws Exception
{
//creates an instance of the digitCount object and starts the run method
DigitCount counter = new DigitCount();
counter.run(args[0]);
}
if charConts doesn't contain i (as in charCounts.get(i)), then it won't throw a NullPointerException, it will return null. Therefore you should be using an if and not a trycatch as in:
curCounts = charCounts.get(i);
if(curCounts==null)
curCounts = new ArrayList<>();
Edit: Alternatively if you are using java 8 you can do
curCounts = charCounts.getOrDefault(i,new ArrayList<Integer>());
and it will automatically default to creating a new ArrayList if it doesn't contain one
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 9 years ago.
Improve this question
This is simple code which should demonstrate point of my question.I don't know if there are some more effective implementations of arrays than this one.What makes them so uncomfortable to me is that we have to write condition into if() block for each element and that makes code much longer.Is there any way how to include all elements of certain array in one line? Thank you!
public class test{
static String[] text;
public static void main(String args[]){
text=new String[5];
text[0]="hello";
...
text[4]="bye";
if(text[0].contains("something")==true||...text[4].contains("something")==true)
//do something
}
}
Either use a loop:
for (String item: text) {
if (item.contains("something")) {
// TODO condition
}
}
... or use an ugly workaround, such as:
if (Arrays.toString(text).contains("something")) {
// TODO condition
}
You can write your own method to do that:
public static anyContain(String[] array, CharSequence sub) {
for (String s : array)
if (s.contains(sub))
return true;
return false;
}
Then the if-statement will be
if (anyContain(text, "something")) {
...
}
I think this is the best way (only for full equals of elements):
text=new String[5];
...
if (Arrays.asList(text).contains("something")) {
// you code here
}
If you need test for contains you can also do:
String[] test = new String[] {"one", "two", "something", "three"};
Arrays.sort(test);
int index = Arrays.binarySearch(test, "something", new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return o1.contains(o2) ? 0 : -1;
}
});
As result you also get an index of element in array. Or negative value if not found.