• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read numbers as input from text file

 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys i am new to java can you guys please help me with my problem
i have input file with nearly 1000 pairs
number pairs.txt
1,2
2,9
3,5.........
and i have to read the input line by line i.e first i have to read the first number pair and have to store it in the array list,then i have to read the next number pair and has to see if any one of the elements are already present or not in the existing array list if present i can leave and should go to next number pair if not i have to store the number pair in new array listhope you guys help me
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
priyanka kusuma wrote:
number pairs.txt
1,2
2,9
3,5.........

1,2,3 is key or index?

priyanka kusuma wrote: if any one of the elements are already present or not in the existing array list if present i can leave and should go to next number pair if not i have to store the number pair in new array list


Not possible in Array list because it allows duplicates , use hashmap instead.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahir Deo wrote:
priyanka kusuma wrote: if any one of the elements are already present or not in the existing array list if present i can leave and should go to next number pair if not i have to store the number pair in new array list


Not possible in Array list because it allows duplicates , use hashmap instead.


Yes, it's possible. He didn't say he wants the list to automatically reject it for him. He said if it's present he'll just move on to the next number. List allows duplicates, but that doesn't mean it forces you to add them if you don't want to.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1,2
2,9
3,5
......
10,9 // Element is present already at 2 so leave this and move on
11,7
.......


Is this what she is trying to say , Jeff ?
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priyanka, you need to be more specific about what you are trying to achieve, please can you supply more detail.

For example do you need to store both numbers in the pair or just the second one? When you are testing for a number pair being already present are you comparing one of the numbers in the pair or both numbers? What do you need to do with the data once you have read it in (this information will almost certainly have a bearing on the most appopriate collection type to use).
 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, what i need is i have to take input from a text file .That input is in the form of
4 29
1 4
5 6
3 9
6 8 and i nearly have 1000 pairs in my file
now i want to take an array list where it has to store 4 and 29 and then it has to come to the 2nd line and has to check for 1 4 whether any of these are present in the array list or not as 4 is already present in the array list it just adds 1 to the arraylist,now it comes to the next line and checks for the next values 5 6 ,as 5 and 6 are not present in the arraylist now 5,6 has to be stored in the new arraylist and it has to check for the next pair 3,9 now we check for 3,9 in both the arrays as it is not present in both the arrays it stores 3 9 in new array list ,and next we have to check for 6,8 as 6 is already present in the 2nd array list 8 is added to the list.
This is the procedure and the output has to be
4 29 1
5 6 8
3 9
 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1,2,3 is key or index?

hey 1,2,3 all these are numbers not any indices these are pairs just like our (x,y) coordinates
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so you are not keeping these as pairs of numbers, you just want to get a list of all the different numbers used.
In that case you should use a Set as Sets do not allow duplicates. With a Set (such as HashSet, LinkedHashSet or TreeSet) you can just put all the numbers in and the collection will only store values that are not already in it.
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priyanka,

Using Sets is the best option if you manually don't want to check every time for the presence of an element, else you could use Arraylist and use .contains() method to check for the presence.

There are various classes implementing Set interface and as said you can use any depending on your need.

If your use-case demands maintaining the insertions order ---- LinkedHashSet is the solution
if it demands Sorted order ---- TreeSet is the solution
and if nothing use HashSet


 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank Gollapudi wrote:Hi Priyanka,

Using Sets is the best option if you manually don't want to check every time for the presence of an element, else you could use Arraylist and use .contains() method to check for the presence.

There are various classes implementing Set interface and as said you can use any depending on your need.

If your use-case demands maintaining the insertions order ---- LinkedHashSet is the solution
if it demands Sorted order ---- TreeSet is the solution
and if nothing use HashSet



Hi Shashank i am using arraylists and this is my code so far

import java.util.ArrayList;
import java.util.Scanner;

public class cluster
{
public static void main(String args[])
{
System.out.println("how many");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
ArrayList<ArrayList<Integer>> x = new ArrayList();
ArrayList y = new ArrayList();
for(int i=1;i<=n;i++)
{
System.out.println("enter pairs");
int a=s.nextInt();
int b=s.nextInt();
if(!y.contains(a) && !y.contains(b))
{ y.add(a);
y.add(b);

x.add(y);

System.out.println(y);
}
else if(y.contains(a)&& !y.contains(b))
{
y.add(b);
x.add(y);
System.out.println(y);

}
else if(!y.contains(a)&& y.contains(b))
{
y.add(a);
x.add(y);
System.out.println(y);

}



}
}
}
but my problem is if my input is:
1 2
2 4
7 8
the expected output is:
1 2 4
7 8
but my output is
1 2 4 7 8
i am unable to store 7,8 in another arraylist any suggestions for this and i dont need any insertion orders
 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Ok so you are not keeping these as pairs of numbers, you just want to get a list of all the different numbers used.
In that case you should use a Set as Sets do not allow duplicates. With a Set (such as HashSet, LinkedHashSet or TreeSet) you can just put all the numbers in and the collection will only store values that are not already in it.

Hi Tony, these are pairs of numbers in simple words if any of the pairs have common elements then they all together should form a group and the pairs that do not have any common elements then they have to be like that
1 2
2 4
7 8
in 1 2 and 2 4 ,2 is common so we combine these two pairs and form 1 2 4 as a group and 7 8 will be another group i hope you understand my point now
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these are pairs of numbers in simple words if any of the pairs have common elements then they all together should form a group

So what happens to:

1 2
3 4
2 3
 
priyanka kusuma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
these are pairs of numbers in simple words if any of the pairs have common elements then they all together should form a group

So what happens to:

1 2
3 4
2 3


1 2
3 4
2 3
output will be
1 2
3 4
because as it comes to the 3rd pair as both the elements are present it does nothing
and here is the algorithm:
1.Take the 1st pair as input from text file and store it in an arraylist
2.now take the next pair from file and check for these conditions:
1. if both the numbers are not present in the arraylist then create a new arraylist and store these two elements in the new array list
2. if any one of these numbers is present in any of the array list then add the other number to that array list.here we have to check in all the arraylists that have been created
3.Continue step 2 until it reaches the end of the file

and i have 2 issues to get resolved:

1.how to take input two numbers at a time from the text file
2.as i have posted the code earlier i am unable to create new arraylist when both the elements in the pair does not exist in the current arrylist
Now i think this is completely about my work i am doing. I hope you understand now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!