Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I tried to put java list into a 2D array and Retrieve it from the JSP. But It wasn't success.I really need this done.. Support me if you can..I have put my codes below if you able to solve this I'm really appreciate that.
Update:
1st List is getting inserted to first row(I want it insert to first column). likewise other three lists inserted to other 3 rows instead of three columns.
Screenshot
Requirement :
Ex:-
Servlet Code.
Scanner scanner = new Scanner(Result);
List<String> cdLine = new ArrayList<String>();
List<Integer> wtc = new ArrayList<Integer>();
List<Integer> ncc = new ArrayList<Integer>();
List<Integer> ccpps = new ArrayList<Integer>();
ControlData controlData = new ControlData();
while(scanner.hasNextLine())
{
token1 = scanner.nextLine();
Wtcs = controlData.CtrlWeight(token1);
NC = controlData.NofConditions(token1);
Ccspps = controlData.previousComplex(token1);
cdLine.add(token1);
wtc.add(Ccspps);
ncc.add(NC);
ccpps.add(Wtcs);
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
}
#SuppressWarnings("rawtypes")
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
scanner.close(); //close the scanner
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/views/Control_structures.jsp");
request.setAttribute("Code_string", arr);
dispatcher.forward(request, response);
JSP code(using JSTL)
<c:forEach items="${Code_string}" var="post" varStatus="theCount">
<tbody>
<c:forEach items="${post}" var="value" varStatus="cell">
<tr>
<td scope="row">${theCount.count}</td>
<td>${value[0]}</td>
<td>${value[1]}</td>
<td>${value[2]}</td>
<td>${value[3]}</td>
<td>-</td>
</tr>
</c:forEach>
</tbody>
</c:forEach>
Update 2:
The 2D array that parsing to the JSP is like this. Hope this also need to be changed.
[[[public class Prime {, if, public static void main(String[]
args) {, , int low = 20, high = 50;, , while (low <
high) {, if(checkPrimeNumber(low)),
System.out.print(low + " ");, , ++low;, }, },
, public static boolean checkPrimeNumber(int num) {,
boolean flag = true;, , for(int i = 2; i <= num/2; ++i) {, ,
if(num % i == 0) {, flag = false;,
break;, }, }, , return flag;, }, }]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]]]
Output that I'm getting:
Thank you for your contribution.
There are two problems in your code:
A. With the following statement, you have created a 3-D structure which is an array of arrays of lists (Note that the List is an implementation of a dynamic 1-D array).
List arr[][]={{cdLine},{wtc},{ncc},{ccpps}};
You need an array of lists as given below:
List arr[]={cdLine,wtc,ncc,ccpps};
B. Do not place the above line inside the while loop. Do it just once after the while loop.
Apart from the points mentioned above, I would recommend you create a custom type instead of using raw type for the List array. Your custom type should be something like:
class MyType {
private List<String> cdLine = new ArrayList<String>();
private List<Integer> wtc = new ArrayList<Integer>();
private List<Integer> ncc = new ArrayList<Integer>();
private List<Integer> ccpps = new ArrayList<Integer>();
// ..constructors and getters and setters
}
Feel free to comment in case of any doubt/issue.
Please, can you post an example to set/write a single bit on a DB ?
With this i write entire byte of the DB (i suppose)
ClientPlc.ReadArea(S7.S7AreaDB, 200, 0, 1, Buffer);
Thanks a lot
byte[] data = new byte[2];
res = client.ReadArea(S7.S7AreaDB, 59001, 0, 2, data); //I want to read DB7.DBD0 (it is a real)
if (res == 0) {
retEmerg01 = S7.GetBitAt(data, 0, 0);
retEmerg02 = S7.GetBitAt(data, 0, 1);
retEmerg03 = S7.GetBitAt(data, 0, 2);
retEmerg04 = S7.GetBitAt(data, 0, 3);
retEmerg05 = S7.GetBitAt(data, 0, 4);
retEmerg06 = S7.GetBitAt(data, 0, 5);
retEmerg07 = S7.GetBitAt(data, 0, 6);
retEmerg08 = S7.GetBitAt(data, 0, 7);
retEmerg09 = S7.GetBitAt(data, 1, 0);
retEmerg10 = S7.GetBitAt(data, 1, 1);
retEmerg11 = S7.GetBitAt(data, 1, 2);
retEmerg12 = S7.GetBitAt(data, 1, 3);
retEmerg13 = S7.GetBitAt(data, 1, 4);
}
You can do so.
For me it works like this:
void WriteBit(int miDB, int miOffset, int miBit, boolean miVal){
if (Connected){
try{
byte[] dataWrite = new byte[1];
Client.ReadArea(S7.S7AreaDB, miDB, miOffset, 1, dataWrite);
//Keeping adjacent values
S7.SetBitAt(dataWrite, 0, miBit, miVal);
Client.WriteArea(S7.S7AreaDB, miDB, miOffset, 1,dataWrite);
}catch(Exception e){println(e);}
}
}
boolean ReadBit(int miDB, int miOffset, int miBit){
boolean miVal = false;
if (Connected){
byte[] dataRead = new byte[1];
Client.ReadArea(S7.S7AreaDB, miDB, miOffset, 1, dataRead);
miVal =S7.GetBitAt(dataRead, 0, miBit);
return miVal;
}
else return false;
}
I am trying to implement a machine learning algorithm (k-nn for example).As it stands my Main class, which essentially builds 8×8-pixel matrices into an array to be manipulated later. (See the data description and sample dataset.) As it stands my arrays are printing as a like so:
, Class Code: 7 DataSet:[0, 0, 3, 9, 15, 8, 0, 0, 0, 1, 15, 16, 16, 7, 0, 0, 0, 0, 5, 16, 16, 10, 4, 0, 0, 0, 3, 16, 16, 16, 9, 0, 0, 0, 0, 15, 14, 4, 0, 0, 0, 0, 0, 13, 5, 0, 0, 0, 0, 0, 1, 15, 3, 0, 0, 0, 0, 0, 4, 13, 0, 0, 0, 0, 7]
Now for my starting point I'm looking to try to implement a very basic kNN algorithm as something to build from but I am having trouble manipulating the datasets that are being outputted. I have been reading up on Foundations of Machine Learning by M. Mohri but it hasn't been of any help. My Main class for building my data:
import java.util.*;
import java.io.*;
public class Main {
static class Data {
int[] dataSet;
int classCode;
public Data(int[] dataSet, int label) {
this.dataSet = dataSet;
this.classCode = label;
}
#Override
public String toString() {
return "Class Code: " + classCode + " DataSet:" + Arrays.toString(dataSet) + "\n";
}
}
ArrayList<Data> dataSetList;
int[][] dataArray = new int[2810][65];
private void readFile(String csvFile) {
int instances = 0;
dataSetList = new ArrayList<>();
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(csvFile)));
while (scan.hasNext()) {
String line = scan.next();
String[] extractedDataFromFile = line.split(",");
for (int i = 0; i < extractedDataFromFile.length; i++) {
dataArray[instances][i] = Integer.parseInt(extractedDataFromFile[i]);
}
dataSetList.add(new Data(dataArray[instances], dataArray[instances][extractedDataFromFile.length - 1]));
instances++;
}
System.out.println(dataSetList.toString());
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
Main main = new Main();
main.readFile("dataset1.csv");
}
}
This is my first time experimenting with machine learning so any feedback or approach to this would be hugely appreciated.
EDIT//
I'm looking at a basic kNN implementation as a starting point whether someone can redirect me to material on implementing on a similar data set or an example using my current provided code. I apologize if my initial post was a little vague
I have three classes and am trying to make a game whereby users move along a grid depending on what is rolled by a die.
I have my main BoardGame class, containing the GUI and the counters which currently are Jlabel images (i'm open to suggestions as to what I could use instead of a JLabel - i wasnt so sure myself). I have a Grid class which I have arranged into a 2D array and called an instance of in the BoardGame class, and I have a die class which rolls a random number from 1-6.
I am trying to get me counters to start at the first square on the grid, and then advance in a left-to-right-right-to-left fashion. I am unsure however of how to make the counters move through the grid in the first place. Hopefully, if I can figure this out, I believe I can then implement them moving a specific amount via the die.
Thanks for the help in advance
GameBoard class:
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel GreenDot;
private JLabel redDot;
private JButton startButton;
private Grid grid;
private Die die;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard(Grid grid) {
super();
this.grid = grid;
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
redDot = new JLabel();
getContentPane().add(redDot);
redDot.setText("jLabel1");
redDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/download.png")));
redDot.setBounds(220, 434, 20, 12);
redDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
GreenDot = new JLabel();
getContentPane().add(GreenDot);
GreenDot.setText("jLabel1");
GreenDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/3d-green-ball-th.png")));
GreenDot.setBounds(222, 453, 21, 13);
GreenDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(64, 443, 83, 23);
}
{
Board = new JLabel();
getContentPane().add(Board);
Board.setLayout(null);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(204, -1, 742, 484);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
Grid class:
public class Grid {
int[][] multi = {
{ 0, 0,-1, 0, 0,-1, 0,-1, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0,-1, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
};
}
Die class:
public class Die {
public Die() {
}
public void dieRoll() {
int SIDES = 6;
int roll = (int) (Math.random() * SIDES) + 1;
System.out.println(roll);
}
}
A simple way of doing this would be to change the die class so it has
{
private int sides;
public Die(int numSides){
sides = numSides;
}
public int roll(){
return (int) (Math.random() * SIDES) + 1
}
}
Then you can roll a six sided die like so
//this creates the die
Die sixSides = new Die(6);
//this rolls the die and prints the roll
System.out.print("That roll got you a " + sixSides.roll());
to move along a 2D array, all you need to do is use the modulus and you need to create a point object class
public position move(int num, int x, int y){//input is dice roll int then current position
for(int i = 0; i < num;i++){
if(i%10==0){
y++;
x = 0;
}else{
x++;
}
}
return new point(x,y);
}
To access the x and y positions of the point object you would need to write get and set methods for the object.
something like this
point player1 = new point(0,0);
int xposition = player1.getX
I have the following code:
public void add (int value)
{
if (numElements == list.length)
System.out.println("Can't add, list is full");
else
{
int hold=0;
for (int j = 0; j < numElements; j++)
{
int temp = list[j];
if (temp <= value)
{
hold = j;
}
}
System.arraycopy(list,hold, list, hold+1 ,numElements-hold);
list[hold] = value;
for (int i = 0; i< list.length; i++)
System.out.print(list[i] + ", ");
System.out.println();
numElements++;
}
}
The purpose is to put an integer in an array at the correct spot smallest to largest(insertion sort).
When I run the following test code:
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
The resulting array is: [25,50,200,100]
When I debug it after each time the method is invoked the arrays are:
[100, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[50, 100, 0, 0, 0, 0, 0, 0, 0, 0]
[50, 200, 100, 0, 0, 0, 0, 0, 0, 0]
[25, 50, 200, 100, 0, 0, 0, 0, 0, 0]
As you can see the error is happening when the 200 is trying to be put in.
I need some help fixing this...thanks!
The arraycopy() call is fine. The problem is that if the correct insertion point is after all elements in the list, your loop will not determine this.
You might try something like:
for(hold = 0; hold < numElements; ++hold) {
if(list[hold] > value) break;
}