The number 1913 appears 1 times.
The number 2016 appears 2 times.
The number 32843 appears 2 times.
The number 31 appears 2 times.
The number 27 appears 2 times.
The number 1.4 appears 2 times.
The number 4 appears 2 times.
The number 7 appears 2 times.
The number 2 appears 2 times.
The number 23 appears 2 times.
The number 23 appears 2 times.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Lecture5Demo2.main(Lecture5Demo2.java:272)
this is the part of my code that I assume I am having issues with. The rest if my code is long using regular expression to extract the numbers from the two different files.
It is also giving me 2 23s and I'm not sure why below are the 2 arraylists
Whole list=[1913, 2016, 1, 1913, 186, 2016, 1711, 32843, 2016, 518, 518, 3, 1913, 32843, 32001, 4, 250, 5, 3500, 6, 7, 7, 8, 27, 73, 9, 10, 1711, 73, 11, 2, 1.4, 1.4, 1.4, 12, 33.75278, 84.38611, 1913, 19, 1930, 20, 21, 1947, 1947, 22, 1955, 23, 1961, 23, 1969, 27, 1995, 26, 27, 27, 1962, 28, 29, 30, 1970, 31, 31]
Whole list=[1913, 2016, 32843, 31, 27, 1.4, 4, 7, 2, 23, 23]
Piet Souris wrote:What kind of ArrayList are you using? (like: ArrayList<Number>?).
Is 'a' incremented in the correct loop, you think?
its an ArrayList <String>
Ive traced it through a few times I believe it is. I swapped them around and my output was completely off :/
 1
A String is still the wrong type to use. If you want numbers, use numbers. Remember that a Scanner can read a text file and convert the tokens to different sorts of number. And that System.out.println or similar can display every kind of number.Natasha Sherrod wrote:I'm using a <string> type because I had to extract the numbers from two different text files . . .
Piet Souris wrote:Okay.
. How many elements does your second list have?
The I loop has 57 elements and the a loop is supposed to have 10 but it has 11 because its doing the 2 23s.
I have it so that it takes the first element for the a loop and goes through the whole i loop counting the reoccurrence. Atleast that's what I'm trying to do and think i have it doing but its only counting 2 of everything and some have more than two. Its like its not running through the whole 57 numbers in the i loop
I do not know where your double 23 is coming from.
Piet Souris wrote:Can you think of a better place?.
I'm not sure I have tried a ton of different things :/ I tried moving it out and putting it in a for loop instead which made the outcome way worse.
currently this is what I have but now its not counting anything they all say 0 times. I sorted the arrays thinking it would make it a bit easier. do I need to switch the placement of the arrays? I have literally tried everything lol (that I can think of)
 1
sometimes a bug IS hard to track down. Your latest code makees things worse, I'm afraid, so let's get back to your original code in your opening post. I added a println to that code, so that you can see where it is going wrong:
(I saved myself some typing by making your lists a little shorter)
So, the error comes from the place where a gets incremented (inside the 'i' loop). So, let's move it out of that loop and see the change:
And finally, let's remove the necessity to increase a 'manually' alltogether, by using a for loop as well:
It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?
Piet Souris wrote:hi Natasha,
It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?
Do I need to convert the arraylist to an int arraylist or double because I have decimals?
I'm not sure why but even with this its still printing out like none of the numbers are in my list
I am still getting this output:
The number 1.4 appears 0times.
The number 1913 appears 0times.
The number 2 appears 0times.
The number 2016 appears 0times.
The number 23 appears 0times.
The number 23 appears 0times.
The number 27 appears 0times.
The number 31 appears 0times.
The number 32843 appears 0times.
The number 4 appears 0times.
The number 7 appears 0times.
Piet Souris wrote:
It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?
Wait!!! I think I did it. I had to change to == to .equals()
just have to count and make sure the numbers are right. it is telling me they are all 3s. so instead of using while then for us for then for to increment without having to say it. still trying to find out exactly where it was messing up before
 1
In your original code, you used a List<String>. To work with that as well would mean I had to type 57 times a double quote around all these numbers. That's why I used two much shorter List<Integer>'s.
But because odf this, I could simply use 'if list1.get(a) == list2.get(i)' making use of Java's automatic boxing and unboxing.
If you use a list<String> then indeed you must use 'equals'. So, that part depends on the type of your lists.
In your list, you use doubles and ints. That's why I initially asked for the type of your numbers, which were strings.
Have you found out how you got 23 twice in your list? Is it twice in your source? If that is a problem, you could use a Set instead of a List here, since Sets do not allow duplicate entries.
Piet Souris wrote:
Have you found out how you got 23 twice in your list? Is it twice in your source? If that is a problem, you could use a Set instead of a List here, since Sets do not allow duplicate entries.
Yes, I think I pretty much understand most of it now. I'm still not sure where the two 23s are coming from I am going to have to go back and look at my code from earlier when I imported the files, sorted the numbers out and added them to list. Its probably in there. Thank you so much for your help.
What is a set? I'm going to google and try it out. maybe see if that will fix the problem with the 2 23s.
Natasha Sherrod wrote:(...)
Yes, I think I pretty much understand most of it now. I'm still not sure where the two 23s are coming from I am going to have to go back and look at my code from earlier when I imported the files, sorted the numbers out and added them to list. Its probably in there. Thank you so much for your help.
You're welcome!
What is a set? I'm going to google and try it out. maybe see if that will fix the problem with the 2 23s.
I would leave it for now. No doubt you will learn about Sets soon enough!
Greetings,
Piet
Why is the word "abbreviation" so long? And this ad is so short?
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
