Reputation: 23
my file.csv contains
**[email protected]**
**[email protected]**
**ibrsduser**
**[email protected]**
**wsdl**
**[email protected]**
**Remedy Application Service**
**[email protected]**
I want to write these contents into another .csv file like:
**[email protected]**, **some random_value()**
**[email protected]**, **some random_value()**
**ibrsduser**,**some random_value()**
**[email protected]**,**some random_value()**
**wsdl**,**some random_value()**
**[email protected]**,**some random_value()**
**Remedy Application Service**,**some random_value()**
**[email protected]**,**some random_value()**
In above lines some random_value() is method that generates random string for first string (i.e [email protected])
this is what i have tried but its printing same random value for all different employee names
this is what i have tried so far
String KeyValue= "";
private static void getString() throws IOException
{
PrintStream out = new PrintStream("D:\\fileUpdate.csv");
BufferedReader br=new BufferedReader(new FileReader("D:\\file.csv"));
String str=null;
while((str=br.readLine())!=null)
{
String fetch=Generate_StringForUSer(str);
out1.println(str+","+fetch);
}
}
private static String Generate_StringForUSer(String str) {
int keylen=str.length();
while (KeyValue.length () != keylen)
{
int rPick = r.nextInt(4);
if (rPick == 0)
{
int spot = r.nextInt(25);
KeyValue += dCase.charAt(spot);
}
else if (rPick == 1)
{
int spot = r.nextInt (25);
KeyValue += uCase.charAt(spot);
}
else if (rPick == 2)
{
int spot = r.nextInt (7);
KeyValue += sChar.charAt(spot);
}
else if (rPick == 3)
{
int spot = r.nextInt (9);
KeyValue += intChar.charAt (spot);
}
}
System.out.println (KeyValue);
return KeyValue;
}
Output is like this....
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
Same random string is generating for all users.. it should be different for different names... please tell me where i am wrong.. thanks to all
Upvotes: 0
Views: 100
Reputation: 5758
You are using a different buffered reader while reading from the file.
Notice the 1
in
while((str=br1.readLine())!=null)
Where as you are declaring a BufferedReader
br
BufferedReader br=new BufferedReader(new FileReader("D:\\file.csv"));
change the above while statement to
while((str=br.readLine())!=null)
To generate a String of given length, containing random characters,
Option 1 Use org.apache.commons.lang 's
RandomStringUtils class.
RandomStringUtils.random(lengthOfRequiredStr)
Option 2 If you wish not to depend on the commons library, Code it yourself - Refer this link
Upvotes: 1