Java : Appending extra tokens dynamically to a String - java

I am parsing an File , sometimes i get a small line or big line .
Is it possible to append Line with extra tokens incase if the line is small ??
This is my program
private static String[] LISTOFFIELDS = null;
String fields = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
String line = "OIUU||HHH|INBVC INB|PP|NN|OO|PPPPP||";
String[] totaltokens = line.split("\\|", LISTOFFIELDS.length);
private static HashMap<String, Object> ParsedValues(String[] totaltokens) {
HashMap<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < LISTOFFIELDS.length; i++) {
String fieldName = LISTOFFIELDS[i];
map.put(fieldName, totaltokens[i]);
}
}
As you can see if the line is small i will get
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Here i can avoid ArrayIndexOutOfBoundsException if i reduce the String fields to String fields = "A|B|C|D|E|F|G|H|";
Is it possible to avoid the Exception , without doing any modifications to the String fields ??

Check the length of the string before going to split and getting its substring.

Just reduce the number of iterations in your loop:
int size = Math.min(LISTOFFIELDS.length, totaltokens.length);
for (int i = 0; i < size; i++) {
String fieldName = LISTOFFIELDS[i];
map.put(fieldName, totaltokens[i]);
}

Related

Get,Put key and values from nested hashmap

I want to create a nested HashMap which returns the frequency of terms among multiple files. Like,
Map<String, Map<String, Integer>> wordToDocumentMap=new HashMap<>();
I have been able to return the number of times a term appears in a file.
Map<String, Integer> map = new HashMap<>();//for frequecy count
String str = "Wikipedia is a free online encyclopedia, created and edited by
volunteers around the world."; //String str suppose a file a.java
// The query string
String query = "edited Wikipedia volunteers";
// Split the given string and the query string on space
String[] strArr = str.split("\\s+");
String[] queryArr = query.split("\\s+");
// Map to hold the frequency of each word of query in the string
Map<String, Integer> map = new HashMap<>();
for (String q : queryArr) {
for (String s : strArr) {
if (q.equals(s)) {
map.put(q, map.getOrDefault(q, 0) + 1);
}
}
}
// Display the map
System.out.println(map);
In my code its count the frequency of the given query Individually. But I want to Map the query term and its frequency with its filenames. I have searched around the web for a solution but am finding it tough to find a solution that applies to me. Any help would be appreciated!
I hope I'm understanding you correctly.
What you want is to be able to read in a list of files and map the file name to the map you create in the code above. So let's start with your code and let's turn it into a function:
public Map<String, Integer> createFreqMap(String str, String query) {
Map<String, Integer> map = new HashMap<>();//for frequecy count
// The query string
String query = "edited Wikipedia volunteers";
// Split the given string and the query string on space
String[] strArr = str.split("\\s+");
String[] queryArr = query.split("\\s+");
// Map to hold the frequency of each word of query in the string
Map<String, Integer> map = new HashMap<>();
for (String q : queryArr) {
for (String s : strArr) {
if (q.equals(s)) {
map.put(q, map.getOrDefault(q, 0) + 1);
}
}
}
// Display the map
System.out.println(map);
return map;
}
OK so now you have a nifty function that makes a map from a string and a query
Now you're going to want to set up a system for reading in a file to a string.
There are a bunch of ways to do this. You can look here for some ways that work for different java versions: https://stackoverflow.com/a/326440/9789673
lets go with this (assuming >java 11):
String content = Files.readString(path, StandardCharsets.US_ASCII);
Where path is the path to the file you want.
Now we can put it all together:
String[] paths = ["this.txt", "that.txt"]
Map<String, Map<String, Integer>> output = new HashMap<>();
String query = "edited Wikipedia volunteers"; //String query = "hello";
for (int i = 0; i < paths.length; i++) {
String content = Files.readString(paths[i], StandardCharsets.US_ASCII);
output.put(paths[i], createFreqMap(content, query);
}

how to convert list<objct> into string []

Is it possible to convert this type List<Jadval> into String[] wordList?
I read the words from database with like this :
public static List<Jadval> jadvalList = new ArrayList<Jadval>();
JadvalDB jadvalDB = new JadvalDB(GameActivity.this);
jadvalList = jadvalDB.getWords(myPos + 1);
and now i want to put jadvalList values into String[] wordList.
i use this code to set the values :
for (int i = 0; i < jadvalList.size(); i++) {
wordList[i] = (jadvalList.get(i).toString());
}
but I get the error that wordList is empty .
any idea?
You can use streams and complete it in one line like this:
String[] wordList = jadvalList.stream().map(a->a.toString()).toArray(String[]::new);

How to to add values from a file into an array using split?

I have the following code:
BufferedReader metaRead = new BufferedReader(new FileReader(metaFile));
String metaLine = "";
String [] metaData = new String [100000];
while ((metaLine = metaRead.readLine()) != null){
metaData = metaLine.split(",");
for (int i = 0; i < metaData.length; i++)
System.out.println(metaData[0]);
}
This is what's in the file:
testTable2 Name java.lang.Integer TRUE test
testTable2 age java.lang.String FALSE test
testTable2 ID java.lang.Integer FALSE test
I want the array to have at metaData[0] testTable2, metaData[1] would be Name, but when I run it at 0 I get testtable2testtable2testtable2, and at 1 I'd get NameageID and OutOfBoundsException.
Any ideas what to do in order to get the result I want?
Just print metaData[i] instead of metaData[0] and split each string by "[ ]+" (that means "1 or more spaces"):
metaData = metaLine.split("[ ]+");
As a result, you will get the following arrays:
[testTable2, Name, java.lang.Integer, TRUE, test]
[testTable2, age, java.lang.String, FALSE, test]
[testTable2, ID, java.lang.Integer, FALSE, test]
The code snippet to the preceding output results:
while ((metaLine = metaRead.readLine()) != null) {
metaData = metaLine.split("[ ]+");
for (int i = 0; i < metaData.length; i++)
System.out.print(metaData[i] + " ");
System.out.println();
}
Also, I've written your task by using Java 8 and Stream API:
List<String> collect = metaRead
.lines()
.flatMap(line -> Arrays.stream(line.split("[ ]+")))
.collect(Collectors.toList());
And, finally, there is the most straight-forward way:
final int LINES, WORDS;
String[] metaData = new String[LINES = 5 * (WORDS = 3)]; // I don't like it
int i = 0;
while ((metaLine = metaRead.readLine()) != null) {
for (String s : metaLine.split("[ ]+")) metaData[i++] = s;
}
Correct your code following line inside the for loop,
System.out.println(metaData[0]);
As
System.out.println(metaData[i]);
Although my answer may not fit completely with your question. But as i can see, your file format is TSV or CSV.
May be you should consider using OpenCSV
for your problem.
The library will handle reading, splitting process for you.

Error checking in java while converting a string to a double

I an tryin to convert a string array to a double array, depending on what weather station was chosen from database - some weather stations have no data, so the whole string array is filled with nulls(12 of them in fact) So obviosly if one of them stations is chosen I get an exception. I realy have no time to write a lot of code to work around it, since I have to submit my work very soon... Is there any solution to catch it and display an error message to user insted of lines and lines explaining an error? Thanks!
Here is the loop to convert a string array into a double array
for(int i = 0; i<12; i++)
{
avMaxTempOptimised[i] = Double.parseDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = Double.parseDouble(avMinTempSplit[i]);
meanTempOptimised[i] = Double.parseDouble(meanTempSplit[i]);
highestTempOptimised[i] = Double.parseDouble(highestTempSplit[i]);
lowestTempOptimised[i] = Double.parseDouble(lowestTempSplit[i]);
maxWindOptimised[i] = Double.parseDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = Double.parseDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = Double.parseDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = Double.parseDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = Double.parseDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = Double.parseDouble(mostSunshineDaySplit[i]);
avMaxTemp2Optimised[i] = Double.parseDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = Double.parseDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = Double.parseDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = Double.parseDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = Double.parseDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = Double.parseDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = Double.parseDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = Double.parseDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = Double.parseDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = Double.parseDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = Double.parseDouble(mostSunshineDay2Split[i]);
}
if you do try/catch outside for loop, you'll stop processing the rest of the loop. Try/catch inside for loop is probably closer, as long as you don't mind the unassigned values left in the array. Something like this might be best, changing Optimized arrays to type Double[]:
for(int i = 0; i<12; i++)
{
avMaxTempOptimised[i] = safeDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = safeDouble(avMinTempSplit[i]);
meanTempOptimised[i] = safeDouble(meanTempSplit[i]);
highestTempOptimised[i] = safeDouble(highestTempSplit[i]);
lowestTempOptimised[i] = safeDouble(lowestTempSplit[i]);
maxWindOptimised[i] = safeDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = safeDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = safeDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = safeDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = safeDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = safeDouble(mostSunshineDaySplit[i]);
avMaxTemp2Optimised[i] = safeDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = safeDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = safeDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = safeDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = safeDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = safeDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = safeDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = safeDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = safeDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = safeDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = safeDouble(mostSunshineDay2Split[i]);
}
...
safeDouble( String str){
return str== null ? null : Double.parseDouble(str);
}
Add in quick null checks where you read Optimized arrays and you're golden.

Java finding substring

I have the following String:
oauth_token=safcanhpyuqu96vfhn4w6p9x&**oauth_token_secret=hVhzHVVMHySB**&application_name=Application_Name&login_url=https%3A%2F%2Fapi-user.netflix.com%2Foauth%2Flogin%3Foauth_token%3Dsafcanhpyuqu96vfhn4w6p9x
I am trying to parse out the value for oauth_token_secret. I need everything from the equals sign (=) to the next ampersand sign (&). So I need to parse out: hVhzHVVMHySB
Currently, I have the following code:
Const.OAUTH_TOKEN_SECRET = "oauth_token_secret";
Const.tokenSecret =
content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET + "="))
+ (Const.OAUTH_TOKEN_SECRET + "=").length(),
content.length());
This will start at the beginning of the oauth_token_string, but it will not stop at the next ampersand. I am unsure how to specify to stop at the end of the following ampersand. Can anyone help me?
The indexOf() methods allow you to specify an optional fromIndex. This allows you to find the next ampersand:
int oauth = content.indexOf(Const.OAUTH_TOKEN_SECRET);
if (oauth != -1) {
int start = oath + Const.OATH_TOKEN_SECRET.length(); // or
//int start = content.indexOf('=', oath) + 1;
int end = content.indexOf('&', start);
String tokenSecret = end == -1 ? content.substring(start) : content.substring(start, end);
}
public static Map<String, String> buildQueryMap(String query)
{
String[] params = query.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params)
{
String[] pair = param.split("=");
String name = pair[0];
String value = pair[1];
map.put(name, value);
}
return map;
}
// in your code
Map<String, String> queryMap = buildQueryMap("a=1&b=2&c=3....");
String tokenSecret = queryMap.get(Const.OAUTH_TOKEN_SECRET);
Using String.split gives a much cleaner solution.
static String getValue(String key, String content) {
String[] tokens = content.split("[=&]");
for(int i = 0; i < tokens.length - 1; ++i) {
if(tokens[i].equals(key)) {
return tokens[i+1];
}
}
return null;
}
Click here for a test drive! ;-)
A much better solution is using the Pattern and corresponding Matcher class.
By using a capturing group you can check and "cut out" the the appropriate substring in one step.

Categories