Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

comparision of arraylist with certain range values  RSS feed

 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i want to store account numbers from keyboard to my arraylist and while storing one by one i want to compare those account number with certain numbers like validation of account number.
for eg:account numbers must be greater than 1111 and less than 8888..if customer enter account number 1000 it must be show some msg and take new account number from that custermer...is it possible single arraylist comparision?..i saw with two array lists but i want to use single arraylist.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want validation of the numbers before they are entered into the List? That is easy enough. What have you achieved so far?
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:is it possible single arraylist comparision?..i saw with two array lists but i want to use single arraylist.
Those questions are not really clear, especially second one. Please explain it in different words, or show us what you saw. Thank you.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not getting my desired output with below code..i want validation on arraylist..i want to take values from keyboard to arraylist and while entering i want to validate and store..i don't know is it possible with single arraylist without taking two array lists.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i take two arraylists then validation happening but not properly.
for eg:
i want exactly 8 account numbers with proper validation. i did not get proper logic.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post that code again with correct indentation; I can see things wrong with it but you obviously can't and I think the indentation may be obscuring your mistakes from you. Don't mix K&R indentation and Allman indentation as you are doing. Choose one style and stick to it rigorously.

Why on earth are you copying things from one List to another? Why don't you simply take the input from the keyboard and ask for repeats whenever you get an incorrect number? Why are you using Lists for account numbers? What happens when the same number is entered twice? You say you are not getting proper logic? What is going wrong?
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example is wrong. Don't follow it. Well, unless you need to have another list with non-validated original inputs.

What you need to do, is to (1)get input, (2)validate, (3)add to list.

Same as in a shop. You check the potatoes, if you like them, you add them to basket. You're not adding them to basket, then checking if these are good, and if they are, transferring/putting to another basket.
We used to use that technique in a country side with my grandmother, we used to collect potatoes and put them by default in one place, then assess them, the ones which are good in one basket, the ones are wrong - to another basket
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't get any program code idea regarding taking arraylist input from keyboard and repeat whenever incorrect occur. could you please provide example program code. i myself want to use arraylist
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:i don't get any program code idea regarding taking arraylist input from keyboard and repeat whenever incorrect occur. could you please provide example program code. i myself want to use arraylist

First of all, all programming tasks starts at the some point, where tasks have to be clearly described and whole problem fully understood. You need to StopCoding (<- link to read) and start analysing problem itself. Probably you'll find this link content also useful WhatNotHow (<- click on it).

After you read them, please tell us what you think, what next steps of yours suppose to be, and what kind of approach you are going to use, then we'll start from there.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:i don't get any program code idea regarding taking arraylist input from keyboard and repeat whenever incorrect occur. could you please provide example program code. i myself want to use arraylist


As Liutauras already mentioned, validation may not be the issue -- it everything else. Arguably, you correctly validated the input (being generous). However, you don't do anything with the input (positive or negative) after you completed your check, and you are always checking all the inputs. This means that with the first invalid input, all future inputs will fail validation.

Also, you always read a customer amount of inputs -- regardless of whether they are valid or not.

Henry
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to read, store, validate internet account numbers of 8 customers in arraylist and minimum and maximum account numbers which can be stored are 1111 and 8888. for that requirement i took two arraylists. one for taking all account numbers and another one to store validation outputs..i'm placing my code below

when i give correct input instead of 8 account numbers 36 numbers storing in new arraylist. i'm not getting correct logic..when i give wrong input that was totally disaster.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:when i give correct input instead of 8 account numbers 36 numbers storing in new arraylist. i'm not getting correct logic..when i give wrong input that was totally disaster.

You told us that already, but you haven't answered yet to Campbell's question, what is the correct logic?

Lets take two scenarios:
1. Assume account number is 1111. What the correct logic suppose to be, please state step by step.
2. Assume account number is 1110. What the correct logic suppose to be, please state step by step.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:
when i give correct input instead of 8 account numbers 36 numbers storing in new arraylist. i'm not getting correct logic..when i give wrong input that was totally disaster.


Everything that I mentioned in my previous post (with the exception of the count) still applies.

Perhaps you should take Liutauras recommendation, and work out / review the algorithm / design. You have serious logic flaws that is probably best envisioned by using pen and paper.

Henry
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, OP has done one of the things I said. Correctly indent the code. Now you have done that, it will be easier to go through the code with a pencil and find where the logic errors are. Printing the contents of the new list may give you a hint:-
System.out.println(newList);

But I still think you are better off only using one List and validating input before you add it.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you are already printing the new List. Read its contents carefully and compare it with what you are entering.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. i don't get logic to take arraylist input from keyboard and repeat when incorrect occur. thats why at least i'm trying with two arraylists first.
2. i'm practicing programs on arraylist
3. we got to give unique account number with mentioned range like 1111 and 8888
4. when we give 1111 that number must be stored in new arraylist
5.when we give 1110 that number should not be stored in new arrraylist and error msg must be shown to user like enter new account number mean count decreases here.
i want to store 8 validated account numbers means when we give wrong input that count must be cancelled.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Because you started coding first.
a) When you go to shop and you take the basket, what you do? Going towards the products shell, take the product, assess, if you like, you put in the basket, if not, you put back and look for another one. Right? Looks logically correct.
b) When you go to shop and you take two baskets (one in a left hand and one in a right hand), what you do? Going towards the products shell, take the product, you put in a basket in a right hand, assess, if you like, you take another same product from the shell and put in another basket in a right hand. If you don't like, you just keep one product you don't like in basket which in a left hand side. Is it correct logic?
2. Good.
3. Ok, we understand that.
4. Why in new?
5. If 1110 should not be stored in new ArrayList, does it have to be stored somewhere else?

[edited] Amended 1. point. Please read it.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. i'm trying with two arraylists..first all numbers will be stored in one arraylist..in second one valid numbers will be stored.
2.i'm skipping invalid numbers ..i won't take them in any arraylist. i understood your's yesterday's basket story but i don't want to take invalid in another basket.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:1. i'm trying with two arraylists..first all numbers will be stored in one arraylist..in second one valid numbers will be stored.
2.i'm skipping invalid numbers ..i won't take them in any arraylist. i understood your's yesterday's basket story but i don't want to take invalid in another basket.

See, those two bold parts contradict to each other.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the contents of the 8‑element list and compare that with the contents of the 36‑element list.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:. . .
3. we got to give unique account number with mentioned range like 1111 and 8888
. . .
You never said anything about numbers being unique before. Why did you not mention that earlier? That additional information probably means your code is beyond rescue.

Sorry, what follows isn't going to sound nice, but it will work.
You need 4 keystrokes. They are in that order: control A (on your code) and delete (3). Then (4) click the mouse on this Java™ Tutorials link. Read what sorts of data structures are available to you. When you know what sort of data structure is suitable, then it will be worth starting again.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, once again.

1. get the number from the keyboard input, store in a variable "accountNumber" not in a list.
2. check if this number is within the range you want it to be
3. if yes, add to the list
3.1 if not, print message "it is not valid" and ask user to enter another number
4. repeat everything again as many times as you have to
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i got my requirement with one array and one arraylist..duplication not considerable. i'm placing my code below. can you please tell me how much efficient that below code.. definitely i will try with two arraylists and single arraylist..i won't disappoint with your feed back please ask me questions if there is any ineffient logic
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:how much efficient that below code
Don't worry about the efficiency now, worry about the correctness.
Follow latest CR advice, he is right, that will be the best at the moment.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i compared 8-element list and 36-element list.
in 36-element list first one occured 8 times, second one 7 times, 3 element 6times, 4 element 5 times like 8 element 1time occured.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the style of using count--; is not at all good. I managed to get 8 numbers into it eventually and got this output:-
java internet.rsv

enter account num :
0000
please enter new account number
9999
please enter new account number
123456
please enter new account number
98765
please enter new account number
9999
please enter new account number
2222
22222
please enter new account number
33333 44444 55555
please enter new account number
please enter new account number
please enter new account number
66666
please enter new account number
2222 2222 2222 2222 2222 2222 2222

account numbers of customers are:
[2222, 2222, 2222, 2222, 2222, 2222, 2222, 2222]
So you end up with 8 numbers but I am not quite convinced they are unique.

At least you appear to have worked out why you are getting 36 numbers and corrected that bit. If you add 1 to 8 you get 36. But as you have seen neither an array nor a List is suitable for making numbers unique. Note that with a Scanner you can enter the input before it is read.
But you are making life unnecessarily difficult for yourself. You seem to be writing code without planning or thought in advance. That will cost you no end of blood sweat and tears which a bit of planning would have saved you. Think and plan your app and you can write it much faster than you are doing at present.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:i compared 8-element list and 36-element list.
in 36-element list first one occured 8 times, second one 7 times, 3 element 6times, 4 element 5 times like 8 element 1time occured.
Forget about all this now. This line "for(Integer value :list)" explains that, because every next time you adding new element and all previous from the 1st array list. Anyway, you fooled at least me by not telling at the beginning that numbers has to be unique, later I even didn't notice that when you mentioned that. Lucky Campbell Ritchie noticed and stopped us from going wrong way round. Your numbers has to be unique, it means, apart from validating, you need to check also if number has not been added already. And for this purpose you need to follow again CR provided tutorial link, so you could find appropriate tool to achieve that. It is worth to go for yourself and read about it, you'll learn a lot.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:i got my requirement with one array and one arraylist..duplication not considerable.
So it means you must use minimum and at most 1 Array and 1 ArrayList? Do we understand correctly you this time?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by, “duplication not considerable”?
Are you supposed to use arrays at all? Have you been told to use a List?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Don't mix K&R indentation and Allman indentation as you are doing.

Thanks for that. I had no idea indent styles had names.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:hello,
i want to store account numbers from keyboard to my arraylist and while storing one by one i want to compare those account number with certain numbers like validation of account number.
for eg:account numbers must be greater than 1111 and less than 8888..if customer enter account number 1000 it must be show some msg and take new account number from that custermer...is it possible single arraylist comparision?..i saw with two array lists but i want to use single arraylist.

I have not followed all this -- but what you want to do is separate your bounds checking (> some number and < some number) from anything else and make it part of input acceptance. The earlier you do it the happier you will be. Just compare the keystrokes to a high constant and low constant before you ever accept it as input. If you also want to avoid storing duplicates, what you would first do is the previous, and if you have a valid value look for a duplicate in your stored values.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a better way to avoid duplicates.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at first instance i did not put unique condition..later i thought its good to put unique condition.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did not fool you vilda sir..i'm just practicing programs..sir why we need to use scanner before we read value..i will learn one new thing.
 
sujesh Katri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will read CR sir link and i will ask if i have any doubts.
 
Liutauras Vilda
Marshal
Posts: 4631
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:i did not fool you vilda sir..i'm just practicing programs..sir why we need to use scanner before we read value..i will learn one new thing.
That is fine, don't worry

The main message I wanted to tell you, that it is always better to create a plan for yourself, what are you going or planning to achieve, and clearly identify requirements. It is important for many reasons, but one of those were good example: at the beginning you did not mentioned that those values has to be unique - so in this case certain approach would be used in order to accomplish this task.
Later on you changed the requirements and said, that values has to be unique, in this matter it changes game rules, as other techniques and approach should be used in order to satisfy this particular requirement. The problem starts at point, that from the beginning when you not identify clearly requirements, and you start implementing your program, and requirements changes within the middle of project, you need to delete all code written, as problem has been understood wrongly from the beginning. Well not understood wrongly, but requirements were misleading.

So, it is very important to think a lot about the problem itself, before you even start writing the code. Need to think what you want to achieve, document it (on a piece of paper), think about the possible requirements change in a future, document it again (on a piece of paper), write down the steps in english (on a piece of paper), revise it, if something overlooked - change it (on a piece of paper). Then every general step split to smaller steps (on a piece of paper), and you do the same until you absolutely clear what has to be done at each step.
When you're done with that and there are no misunderstanding on what has to be done, then you can grab Java or any other programming language and start thinking how you going to do.
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sujesh Katri wrote:at first instance i did not put unique condition..later i thought its good to put unique condition.
Thank you
You were not hiding anything, but you have now seen how awkward it can be to implement new or additional requirements like that. You should get some inspiration how to achieve that most easily from the tutorials link I gave you yesterday, but you need a bit of thought and planning before you implement the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!