How to extract value from massive, using Get Metod? - java

I have this code
public void insertStart_auto_load_test_step_info(int countUsers ) throws SQLException{
String insert_auto_load_test_user_info = "insert into auto_load_test_step_info(id_user_info,start_time,step_id) values(" + getIdValue1()+ getCrokStarTime() + "2";
PreparedStatement pstmt = this.con.prepareStatement(insert_auto_load_test_user_info, Statement.RETURN_GENERATED_KEYS);
pstmt.executeUpdate();
And this Class
public class Info {
private int crok = 0;
private int testId = 0;
private int testCaseId = 1;
private long starTime;
private long crokStarTime;
private long crokFinishTime;
int [] idValue1 = new int[100];
int [] idValue2 = new int[100];
int id_case_info = 0;
public int[] getIdValue2() {
return idValue2;
}
public void setIdValue2(int idValue2, int i) {
this.idValue2[i] = idValue2;
}
public int getId_case_info() {
return id_case_info;
}
public void setId_case_info(int id_case_info) {
this.id_case_info = id_case_info;
}
public int[] getIdValue1() {
return idValue1;
}
public void setIdValue1(int idValue1, int i) {
this.idValue1[i] = idValue1;
}
I need add to my Insert value from this method getIdValue1(), how can I do that? I think, there must be some sort of index, but I don't know how to use it.

Model it after your setIdValue2() method.
// You should check, somewhere, that i is a valid index of idValue2
public int getIdValue2(int i) {
return idValue2[i];
}
And now, you may call your method: int temp = getIdValue2(0); // Get item at first index

Related

How to fix an error: /AccountDemo.java:53: error: missing return statement } ^ 1 error

I am getting an error as follows:
/AccountDemo.java:53: error: missing return statement } ^ 1 error
I have tried everything. How can I fix this problem?
class Account
{
private int number;
public int getNumber()
{
return this.number;
}
public void setNumber(int number1)
{
number=number1;
}
}
public class AccountDemo {
public static void main(String[] args) {
Account[] objArray= new Account[5];
objArray[0].setNumber(7);
objArray[1].setNumber(3);
objArray[2].setNumber(5);
objArray[3].setNumber(4);
objArray[4].setNumber(9);
int accountres= searchAccountByNumber(objArray, 63);
System.out.println("Output after first search: "+accountres);
int accountres1= searchAccountByNumber(objArray, 4);
System.out.println("Output after second search: "+accountres1);
}
public static int searchAccountByNumber(Account[] objArray,int s)
{
int i=0;
for(i=0;i<5;i++)
{
if(objArray[i].getNumber()==s)
{
return i;
}
else
{
return -1;
}
}
}
}
I solved it by assigning a variable to it rather than directly returning it, looks as if it works.
class Account {
private int number;
public int getNumber() {
return this.number;
}
public void setNumber(int number1) {
number = number1; }
}public class AccountDemo {
public static void main(String[] args) {
Account[] objArray = new Account[5];
objArray[0].setNumber(7);
objArray[1].setNumber(3);
objArray[2].setNumber(5);
objArray[3].setNumber(4);
objArray[4].setNumber(9);
int accountres = searchAccountByNumber(objArray, 63);
System.out.println("Output after first search: " + accountres);
int accountres1 = searchAccountByNumber(objArray, 4);
System.out.println("Output after second search: " + accountres1);
}
public static int searchAccountByNumber(Account[] objArray, int s) {
int finalreturn = 0;
int i = 0;
for (i = 0; i < 5; i++) {
if (objArray[i].getNumber() == s) {
finalreturn = i;
} else {
finalreturn = -1;
}
}
return finalreturn;
}
}
Good Luck!

How to store nested JSON Array into a Model

Hello so I have a model called Job. This model collects everything about a specific job. But the way the database relations are layed out means that when I return the job details I get a cargo array nested inside my job object. I want to know how I can store just the name inside the cargo array nested inside my JSON Object, I want to store it into my model.
Please may you take time to consider my situation, I have searched StackOverflow and other sites for a solution but the loops they provide aren't working. Hopefully I can find an answer by posting myself
JSON Object with nested Array:
"jobs": [
{
"id": 103080,
"user_id": 496,
"tracker_jobs_id": 91068,
"game_id": 1,
"pickup_city_id": 72,
"destination_city_id": 128,
"cargo_id": 366,
"pickup_company_id": 16,
"destination_company_id": 18,
"date": "2018-11-03",
"distance_driven": 244,
"load_damage": 7,
"estimated_income": 10956,
"total_income": 10956,
"cargo_weight": 24,
"division_load": 0,
"promotional_delivery_id": null,
"another_driver": 0,
"division_id": null,
"convoy_code": null,
"comments": null,
"created_at": "2018-11-04 00:24:42",
"updated_at": "2018-11-04 00:24:42",
"delete": "false",
"status": null,
"cargo": {
"id": 366,
"name": "Square Tubing",
"price_coef": 1,
"fragility": 0.2,
"wotr_only": 0,
"overweight_dlc": 0
},
.... (THEN IT LOOPS WITH THE NEXT JOB)
My Job Model:
public Job (int id, int user_id, int tracker_jobs_id, int game_id, int pickup_city_id, int destination_city_id,
int cargo_id, int pickup_company_id, int destination_company_id, Date date, int distance_driven, int load_damage,
int estimated_income, int total_income, int cargo_weight, int division_load, int promotional_devlivery_id, int another_driver,
int division_id, String convoy_code, String comments, String delete, String status, JSONArray cargo) {
this.id = id;
this.user_id = user_id;
this.tracker_jobs_id = tracker_jobs_id;
this.game_id = game_id;
this.pickup_city_id = pickup_city_id;
this.destination_city_id = destination_city_id;
this.cargo_id = cargo_id;
this.pickup_company_id = pickup_company_id;
this.destination_company_id = destination_company_id;
this.date = date;
this.distance_driven = distance_driven;
this.load_damage = load_damage;
this.estimated_income = estimated_income;
this.total_income = total_income;
this.cargo_weight = cargo_weight;
this.division_load = division_load;
this.promotional_devlivery_id = promotional_devlivery_id;
this.another_driver = another_driver;
this.division_id = division_id;
this.convoy_code = convoy_code;
this.comments = comments;
this.delete = delete;
this.status = status;
this.cargo = cargo;
}
As you can see I have already attempted storing it as a JSONArray but it ends up just being blank []
How I am storing it from my request:
JSONObject jObj = new JSONObject(response);
JSONArray listJobs = jObj.getJSONArray("jobs");
Gson gson = new Gson();
sUserJobs = new ArrayList<>();
for (int i = 0; i < listJobs.length(); i++) {
try {
Job job = gson.fromJson(listJobs.getJSONObject(i).toString(), Job.class);
sUserJobs.add(job);
} catch (JSONException e) {
e.printStackTrace();
}
}
You need to have a Cargo class as well. Then you need to extract cargo like below while getting Job class an set cargo object to relevant object as well.
Cargo cargo = gson.fromJson(listJobs.getJSONObject(i).getString("cargo").toString(), cargo.class);
change your Job model:
public class Job {
#SerializedName("id") // variable name from server
int id = 0;
#SerializedName("user_id")
int user_id = 0;
#SerializedName("tracker_jobs_id")
int tracker_jobs_id = 0;
#SerializedName("game_id")
int game_id = 0;
#SerializedName("pickup_city_id")
int pickup_city_id = 0;
#SerializedName("destination_city_id")
int destination_city_id = 0;
#SerializedName("cargo_id")
int cargo_id = 0;
#SerializedName("pickup_company_id")
int pickup_company_id = 0;
#SerializedName("destination_company_id")
int destination_company_id = 0;
#SerializedName("date")
String date = "";
#SerializedName("distance_driven")
int distance_driven = 0;
#SerializedName("load_damage")
int load_damage = 0;
#SerializedName("estimated_income")
int estimated_income = 0;
#SerializedName("total_income")
int total_income = 0;
#SerializedName("cargo_weight")
int cargo_weight = 0;
#SerializedName("division_load")
int division_load = 0;
#SerializedName("promotional_devlivery_id")
int promotional_devlivery_id = 0;
#SerializedName("another_driver")
int another_driver = 0;
#SerializedName("division_id")
int division_id = 0;
#SerializedName("convoy_code")
String convoy_code = "";
#SerializedName("comments")
String comments = "";
#SerializedName("delete")
String delete = "";
#SerializedName("status")
String status = "";
#SerializedName("cargo")
Cargo cargo = new Cargo();
public Job (int id, int user_id, int tracker_jobs_id, int game_id, int pickup_city_id, int destination_city_id,
int cargo_id, int pickup_company_id, int destination_company_id, String date, int distance_driven, int load_damage,
int estimated_income, int total_income, int cargo_weight, int division_load, int promotional_devlivery_id, int another_driver,
int division_id, String convoy_code, String comments, String delete, String status, Cargo cargo) {
this.id = id;
this.user_id = user_id;
this.tracker_jobs_id = tracker_jobs_id;
this.game_id = game_id;
this.pickup_city_id = pickup_city_id;
this.destination_city_id = destination_city_id;
this.cargo_id = cargo_id;
this.pickup_company_id = pickup_company_id;
this.destination_company_id = destination_company_id;
this.date = date;
this.distance_driven = distance_driven;
this.load_damage = load_damage;
this.estimated_income = estimated_income;
this.total_income = total_income;
this.cargo_weight = cargo_weight;
this.division_load = division_load;
this.promotional_devlivery_id = promotional_devlivery_id;
this.another_driver = another_driver;
this.division_id = division_id;
this.convoy_code = convoy_code;
this.comments = comments;
this.delete = delete;
this.status = status;
this.cargo = cargo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getTracker_jobs_id() {
return tracker_jobs_id;
}
public void setTracker_jobs_id(int tracker_jobs_id) {
this.tracker_jobs_id = tracker_jobs_id;
}
public int getGame_id() {
return game_id;
}
public void setGame_id(int game_id) {
this.game_id = game_id;
}
public int getPickup_city_id() {
return pickup_city_id;
}
public void setPickup_city_id(int pickup_city_id) {
this.pickup_city_id = pickup_city_id;
}
public int getDestination_city_id() {
return destination_city_id;
}
public void setDestination_city_id(int destination_city_id) {
this.destination_city_id = destination_city_id;
}
public int getCargo_id() {
return cargo_id;
}
public void setCargo_id(int cargo_id) {
this.cargo_id = cargo_id;
}
public int getPickup_company_id() {
return pickup_company_id;
}
public void setPickup_company_id(int pickup_company_id) {
this.pickup_company_id = pickup_company_id;
}
public int getDestination_company_id() {
return destination_company_id;
}
public void setDestination_company_id(int destination_company_id) {
this.destination_company_id = destination_company_id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getDistance_driven() {
return distance_driven;
}
public void setDistance_driven(int distance_driven) {
this.distance_driven = distance_driven;
}
public int getLoad_damage() {
return load_damage;
}
public void setLoad_damage(int load_damage) {
this.load_damage = load_damage;
}
public int getEstimated_income() {
return estimated_income;
}
public void setEstimated_income(int estimated_income) {
this.estimated_income = estimated_income;
}
public int getTotal_income() {
return total_income;
}
public void setTotal_income(int total_income) {
this.total_income = total_income;
}
public int getCargo_weight() {
return cargo_weight;
}
public void setCargo_weight(int cargo_weight) {
this.cargo_weight = cargo_weight;
}
public int getDivision_load() {
return division_load;
}
public void setDivision_load(int division_load) {
this.division_load = division_load;
}
public int getPromotional_devlivery_id() {
return promotional_devlivery_id;
}
public void setPromotional_devlivery_id(int promotional_devlivery_id) {
this.promotional_devlivery_id = promotional_devlivery_id;
}
public int getAnother_driver() {
return another_driver;
}
public void setAnother_driver(int another_driver) {
this.another_driver = another_driver;
}
public int getDivision_id() {
return division_id;
}
public void setDivision_id(int division_id) {
this.division_id = division_id;
}
public String getConvoy_code() {
return convoy_code;
}
public void setConvoy_code(String convoy_code) {
this.convoy_code = convoy_code;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getDelete() {
return delete;
}
public void setDelete(String delete) {
this.delete = delete;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Cargo> getCargo() {
return cargo;
}
public void setCargo(List<Cargo> cargo) {
this.cargo = cargo;
}
}
and Create New Class named: Cargo
public class Cargo {
#SerializedName("id")
int id = 0;
#SerializedName("name")
String name = "";
#SerializedName("price_coef")
int price_coef = 0;
#SerializedName("fragility")
double fragility = 0.0;
#SerializedName("wotr_only")
int wotr_only = 0;
#SerializedName("overweight_dlc")
int overweight_dlc = 0;
public Cargo () {
}
public Cargo (int id, String name, int price_coef, double fragility, int wotr_only, int overweight_dlc) {
this.id = id;
this.name = name;
this.price_coef = price_coef;
this.fragility = fragility;
this.wotr_only = wotr_only;
this.overweight_dlc = overweight_dlc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice_coef() {
return price_coef;
}
public void setPrice_coef(int price_coef) {
this.price_coef = price_coef;
}
public double getFragility() {
return fragility;
}
public void setFragility(double fragility) {
this.fragility = fragility;
}
public int getWotr_only() {
return wotr_only;
}
public void setWotr_only(int wotr_only) {
this.wotr_only = wotr_only;
}
public int getOverweight_dlc() {
return overweight_dlc;
}
public void setOverweight_dlc(int overweight_dlc) {
this.overweight_dlc = overweight_dlc;
}
}
or.. you can post your json to this site and download your models:
http://www.jsonschema2pojo.org/
Target language: Java
Source type: JSON Schema or JSON
Annotation style: Gson

Create an array by passing the txt file and Pass array reference variable to the heapsort method

I need help with my project for data structure course. I am suppose to read 10 employees data stored in a text file called Employee.txt and read them into an array then pass that array into the heapSort method and print the sorted employees into an output file called SortedEmployee.txt. For some reason my program is not working. Can anyone help please?
Class Employee
import java.util.ArrayList;
public class Employee
{
public String empId , empName , empDept , empPos;
public double empSalary;
public int empServ;
Employee()
{
empId = ("");
empName = ("");
empDept = ("");
empPos = ("");
empSalary = 0.0;
empServ = 0;
}
Employee(String id ,String name, double salary, String dept , String pos, int serv)
{
empId = id;
empName = name;
empDept = dept;
empPos = pos;
empSalary = salary;
empServ = serv;
}
public void setId(String id)
{
empId = id;
}
public void setName(String name)
{
empName = name;
}
public void setDept(String dept)
{
empDept = dept;
}
public void setPos(String pos)
{
empPos = pos;
}
public void setSalary(double salary)
{
empSalary = salary;
}
public void setServ(int serv)
{
empServ = serv;
}
public String getId()
{
return empId;
}
public String getName()
{
return empName;
}
public String getDept()
{
return empDept;
}
public String getPos()
{
return empPos;
}
public double getSalary()
{
return empSalary;
}
public int getServ()
{
return empServ;
}
public String toString()
{
String str = "Employee Name : " + empName + "\nEmployee ID : " + empId +
"\nEmployee Deaprtment : " + empDept + "\nEmployee Position : "
+ empPos + "\nEmployee Salary : " + empSalary
+ "\nEmployee Years Served : " + empServ;
return str;
}
public int compareTo(Employee emp)
{
int id = empId.compareToIgnoreCase(emp.empId);
if (id != 0)
return id;
return 0;
}
}
Class HeapSort
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
class zNode
{
private int iData;
public zNode(int key)
{
iData = key;
}
public int getKey()
{
return iData;
}
public void setKey(int k)
{
iData = k;
}
}
class HeapSort
{
private int [] currArray;
private int maxSize;
private int currentSize;
private int currIndex;
HeapSort(int mx)
{
maxSize = mx;
currentSize = 0;
currArray = new int[maxSize];
}
//buildheap
public boolean buildHeap(int [] currArray)
{
int key = currIndex;
if(currentSize==maxSize)
return false;
int newNode = key;
currArray[currentSize] = newNode;
siftUp(currArray , currentSize++);
return true;
}
//siftup
public void siftUp(int [] currArray , int currIndex)
{
int parent = (currIndex-1) / 2;
int bottom = currArray[currIndex];
while( currIndex > 0 && currArray[parent] < bottom )
{
currArray[currIndex] = currArray[parent];
currIndex = parent;
parent = (parent-1) / 2;
}
currArray[currIndex] = bottom;
}
//siftdown
public void siftDown(int [] currArray , int currIndex)
{
int largerChild;
int top = currArray[currIndex];
while(currIndex < currentSize/2)
{
int leftChild = 2*currIndex+1;
int rightChild = leftChild+1;
if(rightChild < currentSize && currArray[leftChild] < currArray[rightChild] )
largerChild = rightChild;
else
largerChild = leftChild;
if( top >= currArray[largerChild] )
break;
currArray[currIndex] = currArray[largerChild];
currIndex = largerChild;
}
currArray[currIndex] = top;
}
//remove max element
public int removeMaxElement(int [] currArray)
{
int root = currArray[0];
currArray[0] = currArray[--currentSize];
siftDown(currArray , 0);
return root;
}
//heapsort
private void _sortHeapArray(int [] currArray)
{
while(currentSize != 0)
{
removeMaxElement(currArray);
}
}
public void sortHeapArray()
{
_sortHeapArray(currArray);
}
//hepify
private int[] heapify(int[] currArray)
{
int start = (currentSize) / 2;
while (start >= 0)
{
siftDown(currArray, start);
start--;
}
return currArray;
}
//swap
private int[] swap(int[] currArray, int index1, int index2)
{
int swap = currArray[index1];
currArray[index1] = currArray[index2];
currArray[index2] = swap;
return currArray;
}
//heapsort
public int[] _heapSort(int[] currArray)
{
heapify(currArray);
int end = currentSize-1;
while (end > 0)
{
currArray = swap(currArray,0, end);
end--;
siftDown(currArray, end);
}
return currArray;
}
public void heapSort()
{
_heapSort(currArray);
}
//main method
public static void main (String [] args) throws IOException
{
HeapSort mySort = new HeapSort(10);
Employee [] myArray = new Employee[10];
String firstFile = ("Employee.txt");
FileReader file = new FileReader(firstFile);
BufferedReader br = new BufferedReader(file);
String secondFile = ("SortedEmployee.txt");
PrintWriter outputFile = new PrintWriter(secondFile);
String[] currArray = new String[10];
String lineContent;
while ((lineContent = br.readLine()) != null)
{
for (int i = 0; i < currArray.length; i++)
{
lineContent = br.readLine();
currArray[i] = String.valueOf(lineContent);
mySort.heapSort();
outputFile.println(mySort);
}
}
outputFile.close();
System.out.print("Done");
}
}
I know that the problem is in the main method in the while loop but I just don't know how to solve it.
Employee.txt:
086244
Sally L. Smith
100000.00
Accounting
Manager
7
096586
Meredith T. Grey
150000.00
Physical Therapy
Doctor
9
875236
Christina R. Yang
190000.00
Cardiology
Resident
10
265893
George A. O'Malley
98000.00
Pediatrics
Attending
7
etc......
You have not implemented Comparable<Employee>. It should like this.
public class Employee implements Comparable<Employee>{
#Override
public int compareTo(Employee emp){
return this.empId.compareToIgnoreCase(emp.empId);
}
}
Simply use Arrays.sort() method to sort an array.

Printing an array of objects in Java

I'm making a phone book and filling it with entries. The entries consist of two Strings for surname and initial, and a telephone number. I'm using an array to store the entries. I'm trying to get the array to print out and I've put toString methods in each class. But when I print out i'm still getting "[LEntry;#8dc8569". I'm not sure what I'm doing wrong. Here's the code.
public class Entry {
String surname;
String initial;
int number;
public Entry(String surname, String initial, int number) {
this.surname = surname;
this.initial = initial;
this.number = number;
}
public String getSurname(){
return surname;
}
public String getInitial(){
return initial;
}
public int getNumber() {
return number;
}
void setNumber(int number){
this.number = number;
}
public String toString(){
return surname+ "\t" +initial+ "\t" +number;
}
}
public class ArrayDirectory {
int DIRECTORY_SIZE = 6;
Entry [] directory = new Entry[DIRECTORY_SIZE];
public void addEntry(String surname, String initial, int num) {
int i = findFreeLocation();
directory[i] = new Entry(surname, initial, num);
}
public void deleteEntry(String surname, String initial) {
int i = findEntryIndex(surname, initial);
directory[i] = null;
}
public void deleteEntry(int number) {
int i = findEntryIndex(number);
directory[i] = null;
}
public int findEntry(String surname, String initial) {
int i;
i = findEntryIndex(surname, initial);
return directory[i].getNumber();
}
public void editNum(String surname, String initial, int number) {
int i;
i = findEntryIndex(surname, initial);
directory[i].setNumber(number);
}
public void print() {
// TODO print array
System.out.println(directory);
}
private int findEntryIndex(String surname, String initial) {
int i;
for (i = 0; i <= DIRECTORY_SIZE; i++)
{
if(directory[i] != null && directory[i].getSurname().equals(surname) && directory[i].getInitial().equals(initial))
{
break;
}
}
return i;
}
private int findEntryIndex(int number) {
int i;
for (i = 0; i <= DIRECTORY_SIZE; i++)
{
if(directory[i] != null && directory[i].getNumber() == number)
{
break;
}
}
return i;
}
private int findFreeLocation() {
int i;
for (i = 0; i < DIRECTORY_SIZE; i++)
{
if(directory[i] == null)
{
break;
}
}
return i;
}
public String toString() {
for(int i = 0 ; i< DIRECTORY_SIZE ; i++){
System.out.println( directory[i] );
}
return null;
}
}
public class Test {
public static void main(String[] args) {
ArrayDirectory phoneBook = new ArrayDirectory();
phoneBook.addEntry("Bigger", "R", 2486);
phoneBook.addEntry("Smaller", "E", 0423);
phoneBook.addEntry("Ringer", "J", 6589);
phoneBook.addEntry("Looper", "T", 6723);
phoneBook.addEntry("Lennon", "B", 4893);
phoneBook.addEntry("Martin", "M", 2121);
phoneBook.print();
}
}
Use Arrays.toString();
Arrays.toString(directory);
when you just print directory, which is an instance of array of type Entry, it doesn't override the toString() method the way you are expecting
Also See
Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

Writing information from SQL database to list

I'm working on my homework and I can't complete one piece of my program ...
I have JTable class which makes table in my code ... i have to write method which takes information from sql database and writes it in list
method MUST look like:
public static List selectAnswers (int questionId) throws SQLException, IOException
following code is written by me:
public static List<AnswerRow> selectAnswers (int questionId) throws SQLException, IOException
{
Connection veza = connectToDatabase();
Properties query = new Properties();
AnswersTableModel atm = new AnswersTableModel();
String selectAnswers = query.getProperty("selectAnswers");
PreparedStatement preparedStatement = veza.prepareStatement(selectAnswers);
ResultSet rs = preparedStatement.executeQuery();
List<AnswerRow> lista = new ArrayList<AnswerRow>();
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
?????????????????????????????????????????????????
}
closeConnectionToDatabase(veza);
return lista;
}
????? field is missing and i dont know what to write there to write information answeText and isRight into AnswerRow class , into AnswerTableModel, into list ...
Code which makes JTable (and is given to me and cannot be changed by my teacher) is here:
package hr.tvz.java.deveti.model;
import java.util.List;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class AnswersTableModel extends AbstractTableModel {
private Object[][] answers;
private String[] columnNames;
public AnswersTableModel (String[] colNames){
super();
columnNames = colNames;
}
public AnswersTableModel() {
super();
this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
this.columnNames[0] = "Odgovor";
this.columnNames[1] = "Točan/Netočan";
}
public java.lang.Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
public int getColumnCount() {
return AnswerRow.TABLE_COLUMNS;
}
public int getRowCount() {
if (answers != null)
return answers.length;
else
return 0;
}
public Object getValueAt(int row, int column) {
return answers[row][column];
}
public String getColumnName(int column){
return columnNames[column];
}
public void setValueAt (Object aValue, int rowIndex, int columnIndex){
answers[rowIndex][columnIndex] = aValue;
}
public boolean isCellEditable (int rowIndex, int columnIndex){
return true;
}
public void addNewRow(){
Object[] o = new Object[] {"", false};
if ((answers == null) || (answers.length == 0)) {
answers = new Object[][] {o};
}else{
Object[][] answersTemp = new Object[answers.length + 1][AnswerRow.TABLE_COLUMNS];
for (int i = 0; i < answers.length; i++)
answersTemp[i] = answers[i];
answersTemp[answersTemp.length - 1] = o;
answers = answersTemp;
}
}
public List<AnswerRow> getAnswerRows() {
List<AnswerRow> list = new ArrayList<>();
for (Object[] oRow : answers) {
AnswerRow row = new AnswerRow();
row.setAnswer((String) oRow[0]);
row.setRight((boolean) oRow[1]);
list.add(row);
}
return list;
}
public void setAnswerRows(List<AnswerRow> answerRows){
if (answerRows.size() == 0 ) {
this.answers = new Object[0][0];
return;
}
this.answers = new Object[answerRows.size()][AnswerRow.TABLE_COLUMNS];
for (int i = 0; i < answers.length; i++){
answers[i][0] = answerRows.get(i).getAnswer();
answers[i][1] = answerRows.get(i).isRight();
}
this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
this.columnNames[0] = "Odgovor";
this.columnNames[1] = "Točno/Netočno";
}
public class AnswerRow {
public static final int TABLE_COLUMNS = 2;
private boolean isRight;
private String answer;
public AnswerRow(){
answer = "";
isRight = false;
}
public AnswerRow(String answer, boolean isRight){
this.answer = answer;
this.isRight = isRight;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer){
this.answer = answer;
}
public boolean isRight(){
return isRight;
}
public void setRight(boolean isRight){
this.isRight = isRight;
}
}
}
Please help me .. thanks !
List<AnswerRow> lista = new ArrayList<AnswerRow>();
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
//Create AnswerRow instance and set values to it and Add it to list.
AnswersTableModel .AnswerRow ansrow = atm.new AnswerRow();
ansrow.setAnswer(answerText);
ansrow.setRight(isRight);
//Add it to list.
lista.add(ansrow);
}
One thing I am not sure is why you have AnswersTableModel and what you do with that.
Use public AnswerRow(String answer, boolean isRight) constructor to create object
AnswerRow ar=null;
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
ar=new AnswerRow(answerText, isRight);
lista.add(ar);
}

Categories