i'm trying to make AST of java code in eclipse.
i'm following example of https://help.eclipse.org/2019-12/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FASTParser.html
As Example of the link, i write my code to create sample AST
But, after run the code, i have ArrayIndexOutOfBoundsException error.
full code and error message is attached below
Does anyone know why this error happened?
my code
import java.util.Map;
import org.eclipse.jface.text.Document;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
public class ASTtest {
static String src = "public class Sample {\r\n" +
" public static void main(String[] args){\r\n" +
" case1();\r\n" +
" case2();\r\n" +
" case3();\r\n" +
" }\r\n" +
" static void case1(){\r\n" +
" int a = 1000;\r\n" +
" int b = 10000;\r\n" +
"\r\n" +
" for (int i = 0; i <100 ; i++) {\r\n" +
" if(a<i){\r\n" +
" a=i;\r\n" +
" }else{\r\n" +
" b--;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" static void case2(){\r\n" +
" int a = 1000;\r\n" +
" int b = 10000;\r\n" +
" for (int i = 0; i <1000 ; i++) {\r\n" +
" if(a<i){\r\n" +
" a=i;\r\n" +
" }else{\r\n" +
" b--;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" static void case3(){\r\n" +
" int a=0;\r\n" +
" int b=120847;\r\n" +
" while(a==10000){\r\n" +
" a++;\r\n" +
" b--;\r\n" +
" b+=200;\r\n" +
" for (int i = 0; i <1000 ; i++) {\r\n" +
" a+=1;\r\n" +
" a-=1;\r\n" +
" int k =1;\r\n" +
" while(k <10000){\r\n" +
" k++;\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
" for (int i = 0; i < 900; i++) {\r\n" +
" a++;\r\n" +
" for (int j = 0; j <100 ; j++) {\r\n" +
" a--;\r\n" +
" a++;\r\n" +
" }\r\n" +
" a--;\r\n" +
" }\r\n" +
" }\r\n" +
"}\r\n" +
"";
public static void main(String[] args) {
char[] source = src.toCharArray();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(source);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
}
}
Error message
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11671)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11924)
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11881)
at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:10286)
at org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolver.java:535)
at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1227)
at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:823)
at ASTtest.main(ASTtest.java:78)
in error message , the problem line (ASTtest.java:78) is this
CompilationUnit result = (CompilationUnit) parser.createAST(null);
The purpose of this program is to build a 10x10 array grid made out of periods (.), allow the user to designate a starting point, and then the program will randomly choose numbers which are assigned to directions for the 'walker' to go. Each time it moves it marks its spot with the next letter in the alphabet (the starting point is marked by A). I haven't done this part yet but I know how I will; if the walker crosses out of the bounds of the array (AKA > 10 or < 0) it will say "you were arrested" and if the variable alpha == 'Z' it way say "You made it home".
The main problem as far as I can tell is with the method processing(), where I draw numbers from getRand (which I have confirmed is working) and then assign them directions which are given to the array. But for some reason when it prints the grid it only shows the starting point, and not the movement of the walker.
Another question I have of lesser importance is if there is a more brief way to display my grid than how I have been (A massive brick of text in a System.out.print)
package walktester;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
class DrunkWalker {
private char[][] walkgrid = new char[10][10];
private static int randNSEW;
private int randomnum;
private int startrow;
private int startcol;
private char alpha = 'A';
private int nextrow;
private int nextcol;
public DrunkWalker(int r, int c) {
startrow = r;
startcol = c;
nextrow = startrow;
nextcol = startcol;
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j++)
walkgrid[i][j] = '.';
}
walkgrid[r][c] = alpha;
}
public static void getRand(){
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100 = (int) (randomNum * 100);
randNSEW = x100 % 4;
}
public int getNextRow(){
return nextrow;
}
public int getNextCol(){
return nextcol;
}
public void processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 1){
nextrow++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 2){
nextcol++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 3){
nextrow--;
walkgrid[nextrow][nextcol] = alpha++;
}
}
}
public char[][] DisplayGrid() {
System.out.print(
walkgrid[0][0] + " " + walkgrid[0][1] + " " + walkgrid[0][2] + " " + walkgrid[0][3] + " " + walkgrid[0][4] + " " + walkgrid[0][5] + " " + walkgrid[0][6] + " " + walkgrid[0][7] + " " + walkgrid[0][8] + " " + walkgrid[0][9] + "\n" +
walkgrid[1][0] + " " + walkgrid[1][1] + " " + walkgrid[1][2] + " " + walkgrid[1][3] + " " + walkgrid[1][4] + " " + walkgrid[1][5] + " " + walkgrid[1][6] + " " + walkgrid[1][7] + " " + walkgrid[1][8] + " " + walkgrid[1][9] + "\n" +
walkgrid[2][0] + " " + walkgrid[2][1] + " " + walkgrid[2][2] + " " + walkgrid[2][3] + " " + walkgrid[2][4] + " " + walkgrid[2][5] + " " + walkgrid[2][6] + " " + walkgrid[2][7] + " " + walkgrid[2][8] + " " + walkgrid[2][9] + "\n" +
walkgrid[3][0] + " " + walkgrid[3][1] + " " + walkgrid[3][2] + " " + walkgrid[3][3] + " " + walkgrid[3][4] + " " + walkgrid[3][5] + " " + walkgrid[3][6] + " " + walkgrid[3][7] + " " + walkgrid[3][8] + " " + walkgrid[3][9] + "\n" +
walkgrid[4][0] + " " + walkgrid[4][1] + " " + walkgrid[4][2] + " " + walkgrid[4][3] + " " + walkgrid[4][4] + " " + walkgrid[4][5] + " " + walkgrid[4][6] + " " + walkgrid[4][7] + " " + walkgrid[4][8] + " " + walkgrid[4][9] + "\n" +
walkgrid[5][0] + " " + walkgrid[5][1] + " " + walkgrid[5][2] + " " + walkgrid[5][3] + " " + walkgrid[5][4] + " " + walkgrid[5][5] + " " + walkgrid[5][6] + " " + walkgrid[5][7] + " " + walkgrid[5][8] + " " + walkgrid[5][9] + "\n" +
walkgrid[6][0] + " " + walkgrid[6][1] + " " + walkgrid[6][2] + " " + walkgrid[6][3] + " " + walkgrid[6][4] + " " + walkgrid[6][5] + " " + walkgrid[6][6] + " " + walkgrid[6][7] + " " + walkgrid[6][8] + " " + walkgrid[6][9] + "\n" +
walkgrid[7][0] + " " + walkgrid[7][1] + " " + walkgrid[7][2] + " " + walkgrid[7][3] + " " + walkgrid[7][4] + " " + walkgrid[7][5] + " " + walkgrid[7][6] + " " + walkgrid[7][7] + " " + walkgrid[7][8] + " " + walkgrid[7][9] + "\n" +
walkgrid[8][0] + " " + walkgrid[8][1] + " " + walkgrid[8][2] + " " + walkgrid[8][3] + " " + walkgrid[8][4] + " " + walkgrid[8][5] + " " + walkgrid[8][6] + " " + walkgrid[8][7] + " " + walkgrid[8][8] + " " + walkgrid[8][9] + "\n" +
walkgrid[9][0] + " " + walkgrid[9][1] + " " + walkgrid[9][2] + " " + walkgrid[9][3] + " " + walkgrid[9][4] + " " + walkgrid[9][5] + " " + walkgrid[9][6] + " " + walkgrid[9][7] + " " + walkgrid[9][8] + " " + walkgrid[9][9] + "\n"
);
return walkgrid;
}
}
public class WalkTester {
public static void main(String[] args) {
Scanner inpr = new Scanner(System.in);
Scanner inpc = new Scanner(System.in);
Scanner inpchoice = new Scanner(System.in);
int r = 0;
int c = 0;
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
System.out.println("Please enter x coordinate between 1 and 10.");
r = inpr.nextInt();
r = r - 1;
System.out.println("Please enter y coordinate between 1 and 10");
c = inpr.nextInt();
c = c - 1;
if(r < 0 || r > 9 || c < 0 || c > 9){
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
DrunkWalker drunkwalker = new DrunkWalker(r, c);
drunkwalker.DisplayGrid();
System.out.println("Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
}
}
The code that moves the walker is in a method called processing(), but you never call it.
You can make your DisplayGrid() method simpler (and it will still print the same stuff):
public char[][] DisplayGrid() {
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
System.out.print(walkgrid[x][y] + " ");
}
System.out.println();
}
return walkgrid;
}
Your processing() method needs to check if the walker leaves the bounds of the area. If you don't, you'll get an ArrayIndexOutOfBoundsException.
public boolean processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
}
if(randNSEW == 1){
nextrow++;
}
if(randNSEW == 2){
nextcol++;
}
if(randNSEW == 3){
nextrow--;
}
if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
return false;
}
walkgrid[nextrow][nextcol] = alpha++;
}
return true;
}
Now you need to call processing() and check the return value to see if the walker succeeded:
DrunkWalker drunkwalker = new DrunkWalker(r, c);
boolean walkerSucceeded = drunkwalker.processing();
drunkwalker.DisplayGrid();
if(walkerSucceeded) {
System.out.println("You made it home");
} else {
System.out.println("You were arrested");
}
And keep in mind (as you will see when you test this), that the walker can cross their own tracks (so you might see some letters missing).
this has me stumped. Im using a loop to go through an array and then using ifs/else to find whether it meets the criteria. However the else statement keeps being executed even though an if is being executed aswell. Probably something really basic im missing. I need to check in the if statements twice as the user is entering a genre aswell as the item in the array meets that genre too.
cheers
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
I suspect what you mean is that you have some entries in the array that do match your search condition, and others that don't. For each match, the appropriate if clause is executing, but for each non-match, the else is executing.
Since the action in the else is to add a message indicating that no matches were found, you probably don't want to add that every time a single non-match is found. Rather, you want to do it once, after the loop, if no matches at all were found. This might look like:
String genreList = "";
for (int i = 0; i < movies.length; i++)
{
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
}
}
if (genreList.equals(""))
{
genreList = "No rental movies were found for the genre: " + genreID;
}
P.S. I don't understand at all why you are checking the genre for each movie against both genreID and a hardcoded value before accepting it. Are there other genres that exist in the list but you don't want a user to be able to search for? That's the only way it seems to make sense, but that doesn't seem like a very sensible requirement.
What is probably happening is that the if statement is not being evaluated correctly. This is what I would try:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
The other thing I would try is to get rid of some of the code, ie:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genres action, childrens, or drama -";
}
}
The third thing I would suggest would be to try this:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") || movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
Try stepping through it with a debugger and you can watch the state of the variables as you go.
I am learning java, and from what I can tell, what I am looking to do is a rare situation.
I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)
What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.
package co.kbcomp.music;
import java.util.*;
import org.jfugue.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number for the seed of the song");
long seed = in.nextLong();
Calendar cal = Calendar.getInstance();
Random rand;
//rand = new Random(cal.getTime());
rand = new Random(seed);
int NoteNumber = 0;
int NoteLength = 0;
int OctiveNumber = 0;
int ChordNumber = 0;
int InversionNumber = 0;
//int Duration = rand.nextInt(100 - 5) + 5;
//This keeps track of the iteration of the for loop.
String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
//This is what is being played
String[] note = { "A", "B", "C", "D", "E", "F", "G" };
String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
String[] chord = { "", "maj", "", "min"};
String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
String[] key = {"",""};
String keys= " ";
String randstr = " ";
//this is the loop that defines the music legnth
for (int i = 0; i < 21; i++) {
NoteNumber = rand.nextInt(7);
NoteLength = rand.nextInt(8);
OctiveNumber = rand.nextInt(18);
ChordNumber = rand.nextInt(4);
InversionNumber = rand.nextInt(10);
NN[i] = note[NoteNumber]; // This randomly generates the note to be played.
NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note.
ON[i] = octive[OctiveNumber]; // This defines the octive to be played in.
CN[i] = chord[ChordNumber]; // This is defines the major or the minor
IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];
key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
//randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
}
System.out.println(key);
Player player = new Player();
Pattern pattern = new Pattern(key[0]);
player.play(pattern);
}
}
You declare your key array with a length of two:
String[] key = {"",""};
But then later in your for loop, you try to access elements beyond the length of your array:
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] +
" " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +
" " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] +
" " + key[19] + " " + key[20]);
Since your array has only a length of two, when you try to access the third element (at array index 2), you get an ArrayIndexOutOfBoundsException.
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
...
key[i] = ....
Do you see the problem?
As you array in only 20 in length
then this
for (int i = 0; i < 21; i++) {
is going to cause an overflow
It should be < 20
Plus this code is meaningless as your key is only an array of 2
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...
You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.
You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...