I am quiet new to java and below is my java code. When I execute this java program I am getting an exception as
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
I have already find out the reason why i am getting error.
When i open this csv file in normal text editor then i dont see any issue with the data. But when i try to open the file in VI editor in Ubuntu then i can see there is ^M line character and this is causing the exception. When i edit the file and remove the ^M and run the program again then its working fine and inserting data into table.
It is the line break on Windows PCs which is being read as ^M in VIM based editors and i am getting this file from windows and i am reading this in ubuntu.
Here is the screenshot where i can see ^M and it is at the index 4.
I see replaceAll function in java but i dont know how to use it and where exactly i need to use it. I only need to remove ^M and read the file.. Please help
I tried with condition String line = line1.replaceAll("^M",""); but still getting same exception. I am not sure is there any other way to handle this in exception or other logic
Based on comments and stack trace, editing the answer;
So here's what's happening;
You have a file with certain number of entries in each row that you are doing some action upon.
But due the contol m character (^M), the java code is not behaving as expected.
Your lines where the control M character is observed is basically split into two separate lines when you are reading it from the bufferedReader.readLine() method.
Now ideally, your file would have the number of columns that is already know to you.
But for the lines with control M character, not the columns have been split (as per explaination above).
In my opinion, you can do either of the two;
You can remove the control M characters from your file, either
manually or through any linux operation (Refer:
remove ^M characters from file using sed)
Change the for loop to run for a limit that is based on columns List instead of the heading List, since columns list is a more appropriate list representing the dynamically split line of the file.
for (int i = 0; i < columns.size(); i++) {
If you go for Option 2, you may also need to change the logic in your loop. Since I am not aware of your DB model and file, I guess you are better equipped to do so.
Related
I am trying to modify a pbit archive in java in my application.
The point is to update the data source of a pbit without using any PowerBI application so I have to modify the DataModelSchema entry.
My problem is : when I read the file with an InputStream and display it in the console, there are blank spaces added between each letter so I am not able to search and replace the right string. Even if I add artificially blank spaces in my string.
For example if i search "content" or even "c o n t e n t", it never finds it.
That problem never appears when I read "normal" .zip archives.
An overview of my ouput when i read the file with additional blank spaces :
https://community.powerbi.com/t5/Service/modify-pbit-data-source-in-a-program/m-p/1744747#M124339
So I would like to get to know if there is a special encoding in pbit templates which might add special spaces or whatever if there is a way to read it properly.
Thanks for your help,
Regards
J.MARQUE
Just to clarify if someone get the same problem: that appears that it is not possible (for now) to update directly the content of the data schema of a pbit template, the only way seems to use Power BI REST API.
I am making an Eclipse plugin and it includes a built-in textEditor implementation, which shall be displaying an SQL query from the database.
However, I want it to only be allowing to create a 72-character-long lines. Therefore, if i write and endless line, I want it to jump into a new line automatically once the line is 72 characters long.
I am now able to load the statement and display it there, cutting the line at 72 characters, but if i edit the query in the editor, it lets me to create a line at any length.
I tried to look for a parameter or a method that would set it, but I failed.
Does anybody have any idea?
Thanks a lot!
Why dont you try using regular expression .Refer the below link for an example.
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet196.java
I have been experimenting with multithreading lately, and I coded an application that downloads a file with HTTPUrlConnection's Range request property. I first get the length of the file from the header, and then I split it up into X number of equal parts, and if there is a remainder, I assign one more thread to take up the slack. Then each of the parts go into an object inside of a queue. Then multiple threads access each task in the queue and execute it, downloading each part concurrently into separate files.
The way that I join the files is the problem. No matter whether I use Linux cat or Windows' copy /B or type, it always comes out that the resulting file comes out invalid in some way.
With AVI files, the index is broken, but when rebuilt, the AVI plays correctly. With .rar files winrar displays "unexpected end of archive", although the files extract normally. What could be causing this. I made sure that no bytes overlapped when I split it up amongst threads.
You're using the Range parameter to the request incorrectly. The end index is for the last character to be read, inclusive, while your algorithm passes the index for the first character that you don't want transferred. Subtract 1 from the argument you pass as the end argument to DownloadPart, and you should be fine:
list.add(new DownloadPart(pos, pos + pieceLen - 1, savePath, url, String.valueOf(ch)));
You've also got some unnecessary duplication of code that you should probably clean up; your first full block doesn't need to be treated any differently than any other full block, which would simplify your code.
I've done a lot of internet searching to find some information to no avail.. Hopefully you can help me..
I want to be able to use a flat file, with normal content (i.e. full english sentences, paragraphs etc), extract each word and store each word individually, one word per row, in a SQL database (doesn't matter if there are spaces but characters such as apostrophes can be kept in)
I then want to have a HTML page with code to access this DB and output the text to the user one word at a time, essentially 'writing' the inputted files text word-by-word on the web page.
This is just a coding exercise but I am frustrated as I know the what but not the how.. I am not sure where to start. Please note some of these files can be quite big ~ 20,000 words so there may be a performance element to consider to any solution.
TL;DR: I want to extract individual words from a text file with normal everyday sentences into a SQL DB that I can retrieve from a HTML page.
Simple read & split exercise
with open(<filename>) as f:
dd = {}
for ln in f:
wds = ln.strip().split()
for word in wds:
dd[word] = 1 # need something for value
for wkey in dd:
<insert into db>
Well, before you start you should choose just one programming language. Since you seem like you are a beginner I would highly recommend Python over Java, but it depends on if you're required to use any particular language by an employer/professor/etc.
Also just to point out, this is also a very BIG task that you've chosen. I'll try to break it down into parts for you, but I recommend starting with just one of these parts before you move on, and make sure it works on your local machine before you try putting it on the web.
First you need to use something read in your file, preferably line by line. A method similar to FileReader/BufferedReader in Java or the open(), readlines() functions in Python will do these. I would also check out the tutorials online on file handling for whichever of these two languages you're going to use. The Python one is here. Practice this with a test file or a small section of your real file before you start working on your real input files.
When you start processing the lines from the file, I would recommend splitting them into individual words using a string split function on spaces or on any punctuation, such as ,.!?". This way you'll pull out the individual words from the each line in the file.
Next, you'll want to choose a database API for the appropriate programming language. I used PyMySQL but there is also MySQLDB for Python. In Java there is JDBC.
You'll need to then build your database on a server somewhere, preferably on the same server as your HTML page for ease of connection. You'll want to practice connecting to your database and adding sample rows before you start trying to process your real input files.
You can't have normal HTML access the database directly - you'll need to use a coding language like Python for that. I've never used Java for webpages, but with Python you'll simply output text and tell the server to display it as the webpage. This will do the trick:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import otherstuffhere
## Must have this header to tell browser how to handle this output
## and must be printed first
print ("Content-Type: text/html\n\n")
## Connect to database here
## Your code to display words from the database goes below here
print (myfield1)
Also remember that when you output your text, you'll need to add all the HTML tags to the normal text output. For example, when printing each word, you'll need to add <p> or <br> to end each line, because although the Python print() function will automatically add a line break, this doesn't translate to a line break in HTML. For example:
print ("My word list is: <br>")
for word in dbOutputList:
print (word)
print ("<br>")
After that the REAL fun/crying begins, but you should work on the above before you move on.
I'm using Java to code Data Mining application.
To do so I'm reading a "arff" file and run it over a model created in WEKA.
Currently I have a ARFF file with one line as the data to process and its working good.
What i'm trying to achieve is: that user input from jCheckbox or something some information, and i'll use to to run on the model. I've taught of two ways to do it.
1. Read a file without the last line, and only append it directly to the variable.
2. Delete line number (same place always) from the ARFF, build a line from user inputs, write it to ARFF, and then read the ARFF again.
Any suggestion on which (I think 1 is better) and in general how ?
Tried some code with StringBuilder but no success.