read numbers and strings separated by comma from file (java) [closed] - 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 2 years ago.
Improve this question
i have file
Which contains numbers and strings in this form
1,2,ssh,4,5
sor,7,8,bbt,10
How can I read the numbers/string one by one in a loop?
I tried to read like that:
input = new Scanner(data_to_insert);
String data;
while (input.hasNext()) {
data = input.next();
data = data.replaceAll(",", "");
println("data to hash ->" + data);
But I get for example:
data to hash ->12345
And I wanted to get first 1 after that 2 and so on
like this:
data to hash ->1
data to hash ->2
.
.
.
data to hash ->5

Use a for loop in your while loop to loop through everything in each line (and String.split(","); to separate by the commas. In your example code, you printed out each line instead of each comma-separated token.
input = new Scanner(data_to_insert);
String data;
while (input.hasNext()) {
String[] words = input.next().split(",");
for (String word : words) System.out.println("data to hash -> " + data);
}
This should work.

Related

How to find the Index of string in java array from the given string value [closed]

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 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.

How can I rea a specific column in a CSV file using java? [closed]

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 1 year ago.
Improve this question
I have a CSV file (as shown), how can I read a specific column (for example: age) using java and store it's values only without the header in an ArrayList ?
I do not want to use any dependencies Like OpenCSV or other:
age
data
numbers
111
3434
2343
3444
2232
32332
this should do the job if you want to use the default format you can use ';' as a column separator.
public static List<String> readOnlyOneColumn(String pathToCsv,String columnSeparator,int columnIndex)
throws IOException
{
return Files.lines(Paths.get(pathToCsv)).skip(1).map(e -> e.split(columnSeparator)).map(columns -> columns[columnIndex]).collect(Collectors.toList());
}
It's basically a simple, elegant one liner
Files.readAllLines(Path.of("/path/to/your.csv")).stream().skip(1) //skip header
.map(l -> l.split(";")[0]) // get first column i.e. age
.collect(Collectors.toList()); // and get it as List
First check our csv file format, exists a lot of custom formats.
"String Header";"Numeric Header"
"this is a string value";154566.52
String Header;Numeric Header
this is a string value;154566.52
String Header\tNumeric Header
this is a string value\t154566.52
Files.readAllLines(Path.of("/path/to/your.csv")).stream().skip(1) //skip header
.map(l -> l.split(";")[0]) // get first column i.e. age
.collect(Collectors.toList()); // and get it as List
BufferedReader reader = BufferedReader(new FileInputStreamReader("/path/to/your.csv"));
reader.readLine();
valuesArr = new ArrayList();
while(reader.ready()) {
String line = reader.readLine();
valuesArr.add(line.split(";")[0]);
}
this 2 ways will output respectively results on :
["\"this is a string value\""]
["this is a string value"]
["this is a string value\t154566.52"]

how to remove multiple characters by their indice from a string in java? [closed]

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
How can I remove multiple characters by their index from a string. I thought to use StringBuilder's deleteCharAt function. But if I delete char one by one, I will not keep track the right index.
For example :
String test = "0123456789"
int[] removingIndice = int[] {2, 4, 0};
// remove character from string
// result = "1356789"
Create a new string builder,
iterate on string, add elements to builder if its index not in the array
StringBuilder sb = new StringBuilder("");
for(int i = 0; i< test.length; i++){
if( !ArrayUtils.contains(removingIndice, i))
{
sb.append(test.charAt(i));
}
}
test = sb.toString();
String is immutable in Java, so you will need to create a new String with characters at positions 0,2,4 removed. As one option, you may use StringBuilder for that, as answered here: How to remove single character from a String
I think, you need to redesign the task:
"new string must contains all characters except ..."
Now, seems weak initial data structure and the goal.

Taking values from multiple arraylist in order [closed]

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 9 years ago.
Improve this question
I have 4 array list
ArrayList<String[]> split_url = new ArrayList<String[]>();
ArrayList<String> mediaID = new ArrayList<String>();
ArrayList<String> productID = new ArrayList<String>();
ArrayList<String> clusResult = new ArrayList<String>();
and each arraylist contains some values. i want to take first value from each arralist and save that into one row in database then take second values from each database and save that to next row and so on.... how can i do that ?
1) first initialize all the array lists with zeros upto a certain length l.
2) add some values to each array list.
3) run the a loop like this,
for(int x=0;x<l;x++)
{
String a=split_url(x);
String b=mediaId(x);
String c=productId(x);
String d=clusResult(x);
// now store a,b,c,d as a record in your database;
}

Removing periods from strings inside tables. easy. - java [closed]

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 8 years ago.
Improve this question
I have this string I got from a table via 'string.split(" ");' the problem I have is one of them should include a period. How would I go about searching for this period. I do know I have to search for it because I code in Lua. Though we used String.find() methods.
What is the way I would remove a period from a string in a String[] table?
Thanks in advance :)
This is for school.
If I understand you correctly, you have a String that you've split by delimiting on a space. You now want to search for a period in the resultant array of Strings.
for(String s : stringArray) {
if (s.contains(".")) {
//do something
}
}
Unfortunately, I'm not that clever with RegExp, but you could...
String s = "This.is.a.test";
while (s.contains(".")) {
s = s.replace(".", "");
}
System.out.println(s);
I'm sure there's a wonderful single line RegExp to do the same thing
Updated based on comments
Because String is not mutable, you will need to reassign it back to the original array
String[] apples = {"one", "two.", "three"};
for (int index = 0; index < apples.length; index++) {
String s = apples[index];
while (s.contains(".")) {
s = s.replace(".", "");
}
apples[index] = s;
}
for (String s : apples) {
System.out.println(s);
}

Categories