Below is what I have been attempting to do.

retrive number of rows from MySql table;
for(int i = 1; i <= numberOfRows; i++){
	retrive userID, level at position(i) in table;
	// next i need to insert userID into array, level amount of times (ex. userID = 4, level = 6, insert [4,4,4,4,4,4] into array)
	while (j < level + numberOfItemsInArrayPrior){
		hugeArray[j] = userID;
		j++;
	}
	numberOfItemsInArrayPrior += level;
}

I finally got it to print out without errors, but it is printing a ton of zeros between numbers when i print the array.

static int maxIndex;
	static int numberOfEntries;
	static int[] mainArray;
	static int userID;
	public static void main(String[]args){
		try{
		   	Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
		   	Statement st=con.createStatement();
                    // Selects the highest index of X so all Xs get stored into array
		   	ResultSet rs = st.executeQuery("SELECT MAX(userIDnum) FROM userInfo");
		   	
		   	while(rs.next()){
		   		maxIndex = (Integer) rs.getObject(1);
		   		mainArray = new int[maxIndex];
		   		
		   	}
		   	
		   	int temp = 0;
		   	int j = 0;
		   	for (int i = 0; i <= maxIndex; i++){
                            // Selects amount of times to insert given X into array  
		   		ResultSet rs2 = st.executeQuery("select level, userIDnum from userInfo where userIDnum = '"+i+"'");
		   		while(rs2.next()){
			   		numberOfEntries = (Integer) rs2.getObject(1);
			   		userID = (Integer) rs2.getObject(2);
			   		
			   	}
		   		
		   		
		   		while(j < numberOfEntries + temp){
		   			mainArray[j+temp] = userID;
                                    j++;
		   		}
                            temp += numberOfEntries;
                            
		   	}
		   	
	   	}catch(Exception e1){
	   		System.out.println(e1);
	   	}
	   	for (int k = 0; k < mainArray.length; k++){
    		   System.out.println(mainArray[k]);
	   	}
	}

How do i fix this?

Thanks,
oldezwe

what do you mean by 'between numbers'? on the same line? on a separate line?
what is your input, what is your output and what exactly is the output you expect?

using the data table...

userIDnum     level     username
1             2         John
2             5         Bob
3             3         Chuck

The output I am looking for is
[1,1,2,2,2,2,2,3,3,3]

The output I am getting is
[1,1,0,0,0,1,0,1,0,0,0,0,2,2,2,2,2,0,0,0,2,2,0,0,2,2,0,0,0,0,0,3,3,3,0,0,0,0,3,0,3,0,0]

Edited 4 Years Ago by oldezwe: n/a

have you tried running your code without the temp variable?

I'm not sure what you're trying to do with it, but I think that might be a part of the problem.

This article has been dead for over six months. Start a new discussion instead.