Read text line and put in to the JTable - java

hey
i have text file shown as below.
11/2/2010 cat 6
11/2/2010 cat 3
11/2/2010 dog 4
11/2/2010 cat 11
11/3/2010 cat 1
11/3/2010 dog 3
11/3/2010 cat 8
i have in every month this kind of text file. Above figure shows the part of the text file. so then i want to read this text using java to Jtable.
i Have used StringTokanizer And Arreaylist to ceate this. Unfotunately i couldn't done it. SO PLS HELP ME........
So i want below result to jTable using java program.
date animal total count
11/2/2010 cat 20 3
11/3/2010 cat 9 2

You don't need a StringTokenizer (in fact, it's not recommended). Just get the input line by line using BufferedReader, and do a String split:
List<Array> data = new ArrayList<Array>();
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
String line;
// Read input and put into ArrayList of Arrays
while ((line = in.readLine) != null) {
data.add(line.split("\\s+"));
}
// Now create JTable with Array of Arrays
JTable table = new JTable(data.toArray(), new String[] {
"date", "animal", "total", "count"});

test with : http://crysol.org/es/node/819
or
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class Reader {
public Reader() {
// TODO Auto-generated constructor stub
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream("sample.txt")));
Map<String, Object[]> result = new LinkedHashMap<String, Object[]>();
while (reader.ready()) {
String line = reader.readLine();
String[] values = line.split("\\s+");
String key = values[0] + "\t" + values[1];
String label = values[0];
String date = values[1];
Integer sum = 0;
Integer count = 0;
if (result.containsKey(key)) {
sum = (Integer) ((Object[]) result.get(key))[2];
count = (Integer) ((Object[]) result.get(key))[3];
} else {
}
result.put(key, new Object[]{label, date,
sum + Integer.parseInt(values[2]), count + 1});
}
ArrayList arrayList = new ArrayList(result.values());
/* interate and print new output */
/*
* for (String key : result.keySet()) { Integer sum =
* result.get(key); Integer count = result2.get(key);
* System.out.println(key + " " + sum + "\t" + count); }
*/
JTable table = new JTable(new AnimalTableModel(arrayList));
panel.add(new JScrollPane(table));
reader.close();
frame.setContentPane(panel);
frame.setVisible(true);
frame.pack();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Reader();
}
public class AnimalTableModel implements TableModel {
final Class[] columnClass = new Class[]{String.class, String.class,
Integer.class, Integer.class};
final String[] columnName = new String[]{"Date", "Animal", "Sum",
"Count"};
List values = null;
public AnimalTableModel(List values) {
this.values = values;
// initilize values
}
#Override
public void addTableModelListener(TableModelListener l) {
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return columnClass[columnIndex];
}
#Override
public int getColumnCount() {
return columnClass.length;
}
#Override
public String getColumnName(int columnIndex) {
return columnName[columnIndex];
}
#Override
public int getRowCount() {
return values.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return ((Object[]) values.get(rowIndex))[columnIndex];
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
#Override
public void removeTableModelListener(TableModelListener l) {
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// TODO FOR EDITABLE DT
}
}
}

You will have to populate the data to a map from the given file.I will give you an example as to how to populate the data.
public class AnimalMapping {
public static void main(String[] args) {
Object[][] data = { { "11/2/2010", "cat", 6 },
{ "11/2/2010", "cat", 3 }, { "11/2/2010", "dog", 4 },
{ "11/2/2010", "cat", 11 }, { "11/3/2010", "cat", 1 },
{ "11/3/2010", "dog", 3 }, { "11/3/2010", "cat", 8 } };
HashMap<String, Map<String, AnimalValCnt>> animalMap = new HashMap<String, Map<String, AnimalValCnt>>();
for (Object[] record : data) {
Map<String, AnimalValCnt> innerMap = null;
if ((innerMap = animalMap.get(record[0])) == null) {
innerMap = new HashMap<String, AnimalValCnt>();
animalMap.put((String) record[0], innerMap);
}
AnimalValCnt obj = null;
if ((obj = innerMap.get(record[1])) == null) {
obj = new AnimalValCnt();
innerMap.put((String) record[1], obj);
}
obj.Sumval += (Integer) record[2];
obj.cnt++;
}
System.out.println(animalMap);
}
}
class AnimalValCnt {
int Sumval;
int cnt;
#Override
public String toString() {
return "(" + Sumval + "," + cnt + ")";
}
}
Once you have got the data in a map then it's easy to populate these data to a table.You can use a tablemodel for this purpose.Have a look at this code to understand how data from a map can be loaded into a table using TableModel.
UPDATE:
public class AnimalMapping {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
String line;
String[] record;
HashMap<String, Map<String, AnimalValCnt>> animalMap = new HashMap<String, Map<String, AnimalValCnt>>();
while(((line = in.readLine()) != null)) {
record=line.split("\\s+");
Map<String, AnimalValCnt> innerMap = null;
if ((innerMap = animalMap.get(record[0])) == null) {
innerMap = new HashMap<String, AnimalValCnt>();
animalMap.put(record[0], innerMap);
}
AnimalValCnt obj = null;
if ((obj = innerMap.get(record[1])) == null) {
obj = new AnimalValCnt();
innerMap.put(record[1], obj);
}
obj.Sumval += Integer.valueOf(record[2]);
obj.cnt++;
}
System.out.println(animalMap);
}
}
class AnimalValCnt {
int Sumval;
int cnt;
#Override
public String toString() {
return "(" + Sumval + "," + cnt + ")";
}
}
#Dilantha Chamal: Reading from file was already given by Box9.I just included it in my code.You should be putting some effort here.Maybe you are a beginner that's why I wrote the code.Now try to implement the TableModel by yourself.Just a friendly advice:unless you do it you are never going to learn.

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
public class AnimalSummaryBuilder
{
private static final Splitter SPLITTER = Splitter.on(CharMatcher.anyOf(","));
private static final Joiner JOINER = Joiner.on("\t");
#SuppressWarnings("unchecked")
public static void main(final String[] args) throws Exception
{
#SuppressWarnings("rawtypes")
Map<Animal, Summary> result = Files.readLines(new File("c:/1.txt"), Charsets.ISO_8859_1, new LineProcessor() {
private final Map<Animal, Summary> result = Maps.newHashMap();
public Object getResult()
{
return result;
}
public boolean processLine(final String line) throws IOException
{
Iterator<String> columns = SPLITTER.split(line).iterator();
String date = columns.next();
String name = columns.next();
int value = Integer.valueOf(columns.next()).intValue();
Animal currentRow = new Animal(date, name);
if (result.containsKey(currentRow))
{
Summary summary = result.get(currentRow);
summary.increaseCount();
summary.addToTotal(value);
}
else
{
Summary initialSummary = new Summary();
initialSummary.setCount(1);
initialSummary.setTotal(value);
result.put(currentRow, initialSummary);
}
return true;
}
});
for (Map.Entry<Animal, Summary> entry : result.entrySet())
{
Animal animal = entry.getKey();
Summary summary = entry.getValue();
System.out.println(JOINER.join(animal.date, animal.name, summary.total, summary.count));
}
}
final static class Animal
{
String date;
String name;
public Animal(final String date, final String n)
{
this.date = date;
this.name = n;
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (!(obj instanceof Animal))
{
return false;
}
Animal other = (Animal) obj;
if (date == null)
{
if (other.date != null)
{
return false;
}
}
else if (!date.equals(other.date))
{
return false;
}
if (name == null)
{
if (other.name != null)
{
return false;
}
}
else if (!name.equals(other.name))
{
return false;
}
return true;
}
}
final static class Summary
{
private int total;
private int count;
void setTotal(int value)
{
total = value;
}
void setCount(int i)
{
count = i;
}
void increaseCount()
{
count++;
}
void addToTotal(int valueToAdd)
{
total += valueToAdd;
}
}
}

Related

Hive converting array<string, string> to array<struct<key:string, value:string>> with custom udf

I need to create a custom UDF in hive to convert array<map<string, string>> into array<struct<key:string, value:string>>
I am trying with the following class:
import java.util.List;
import java.util.Map;
import com.google.common.collect.Lists;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.io.Text;
public class ArrayOfMapToArrayOfStructUdf extends GenericUDF {
private static final String UDF_NAME = "convertArrayMapToArrayStruct";
#Override
public String getUdfName() {
return UDF_NAME;
}
#Override
public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
if (objectInspectors.length != 1) {
throw new UDFArgumentLengthException(UDF_NAME + " takes 1 argument of type array<map<key, value>>");
}
if (!(validateArgumentType(objectInspectors))) {
throw new IllegalArgumentException("Code should never reach this section!");
}
return createReturnObjectInspector();
}
private boolean validateArgumentType(ObjectInspector[] objectInspectors) throws UDFArgumentException {
if (!(objectInspectors[0] instanceof ListObjectInspector)) {
throw new UDFArgumentException("the argument must be of type: array<map<key, value>>");
}
ListObjectInspector listObjectInspector = (ListObjectInspector) objectInspectors[0];
if (!(listObjectInspector.getListElementObjectInspector() instanceof MapObjectInspector)) {
throw new UDFArgumentException("the array contents must be of type: map<key, value>");
}
return true;
}
private ObjectInspector createReturnObjectInspector() {
List<String> structFieldNames = Lists.newArrayList("key", "value");
List<ObjectInspector> structFieldObjectInspectors =
Lists.newArrayList(PrimitiveObjectInspectorFactory.javaStringObjectInspector,
PrimitiveObjectInspectorFactory.javaStringObjectInspector);
StructObjectInspector structObjectInspector =
ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors);
return ObjectInspectorFactory.getStandardListObjectInspector(structObjectInspector);
}
#Override
public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
if (deferredObjects == null || deferredObjects.length < 1) {
return null;
}
List<Map<String, String>> arrayOfMap = (List<Map<String, String>>) deferredObjects[0].get();
if (arrayOfMap == null) {
return null;
}
List<Object> arrayOfStruct = Lists.newArrayList();
for (Map<String, String> map : arrayOfMap) {
Object[] object = new Object[2];
object[0] = new Text(map.get("key"));
object[1] = new Text(map.get("value"));
arrayOfStruct.add(object);
}
return arrayOfStruct;
}
#Override
public String getDisplayString(String[] strings) {
return UDF_NAME;
}
}
And I'm getting the following error:
Failed with exception java.io.IOException:org.apache.hadoop.hive.ql.metadata.HiveException: Error evaluating convertArrayMapToArrayStruct
I dont know how to build the object to return in the evaluate method.
The column im trying to transform has data as follows:
[{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}, ..., {"key": "keyN", "value": "valueN"}]
Thanks!
This worked:
#Override
public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
if (deferredObjects == null || deferredObjects.length < 1) {
return null;
}
LazyArray lazyArray = (LazyArray) deferredObjects[0].get();
if (lazyArray == null) {
return null;
}
List<Object> lazyList = lazyArray.getList();
List<Object> finalList = Lists.newArrayList();
for (Object o : lazyList) {
LazyMap lazyMap = (LazyMap) o;
String key = "";
String value = "";
for (Map.Entry<?, ?> entry : lazyMap.getMap().entrySet()) {
if (entry.getKey().toString().equals("key")) {
key = entry.getValue().toString();
} else if (entry.getKey().toString().equals("value")) {
value = entry.getValue().toString();
}
}
finalList.add(Lists.newArrayList(key, value));
}
return finalList;
}

How to store an ArrayList in a file?

I have a class which represents an ArrayList stored in a file, because I need an ArrayList with multiple gigabytes of data in it which is obviously too large to be stored in memory. The data is represented by a class called Field and the function Field.parse() is just for converting the Field into a String and the other way.
The Field class stores a list of (strange) chess pieces and their coordinates.
My class is working fine, but it takes a long time to add an element to the file and I need my program to run as fast as possible. Does anyone know a more efficient/faster way of doing things?
Also, I am not allowed to use external libraries/apis. Please keep that in mind.
This is the class which is responsible for storing Field objects in a temp file:
private File file;
private BufferedReader reader;
private BufferedWriter writer;
public FieldSaver() {
try {
file = File.createTempFile("chess-moves-", ".temp");
System.out.println(file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(Field field) {
try {
File temp = File.createTempFile("chess-moves-", ".temp");
writer = new BufferedWriter(new FileWriter(temp));
reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine()) != null ) {
writer.write(line);
writer.newLine();
}
reader.close();
writer.write(field.parse());
writer.close();
file.delete();
file = new File(temp.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
public Field get(int n) {
try {
reader = new BufferedReader(new FileReader(file));
for (int i = 0; i < n; i++) {
reader.readLine();
}
String line = reader.readLine();
reader.close();
return Field.parse(line);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
And this is the Field class:
private WildBoar wildBoar;
private HuntingDog[] huntingDogs;
private Hunter hunter;
private int size;
#Override
public String toString() {
String result = "Wildschwein: " + wildBoar.toString();
for (HuntingDog dog : huntingDogs) {
result += "; Hund: " + dog.toString();
}
return result + "; Jäger: " + hunter.toString();
}
#Override
public boolean equals(Object obj) {
if (obj instanceof Field) {
Field field = (Field) obj;
HuntingDog[] dogs = field.getHuntingDogs();
return wildBoar.equals(field.getWildBoar()) && hunter.equals(field.getHunter()) && huntingDogs[0].equals(dogs[0]) && huntingDogs[1].equals(dogs[1]) && huntingDogs[2].equals(dogs[2]);
}
return false;
}
public Field(int size, WildBoar wildBoar, HuntingDog[] huntingDogs, Hunter hunter) {
this.size = size;
this.wildBoar = wildBoar;
this.huntingDogs = huntingDogs;
this.hunter = hunter;
}
public WildBoar getWildBoar() {
return wildBoar;
}
public HuntingDog[] getHuntingDogs() {
return huntingDogs;
}
public Hunter getHunter() {
return hunter;
}
public int getSize() {
return size;
}
public static Field parse(String s) {
String[] arr = s.split(",");
WildBoar boar = WildBoar.parse(arr[0]);
Hunter hunter = Hunter.parse(arr[1]);
HuntingDog[] dogs = new HuntingDog[arr.length - 2];
for(int i = 2; i < arr.length; i++) {
dogs[i - 2] = HuntingDog.parse(arr[i]);
}
return new Field(8, boar, dogs, hunter);
}
public String parse() {
String result = wildBoar.parse() + "," + hunter.parse();
for(HuntingDog dog : huntingDogs) {
result += "," + dog.parse();
}
return result;
}
Here's an MCVE to do what you want, based on the information you provided.
You can run it and see that it can save a Field to the file and get a Field by index very quickly.
The Fields are constant length, so you can get a Field by index by going to byte offset of index times field length in bytes. This would be significantly more difficult if the field were not constant length.
import java.io.Closeable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FieldSaver implements Closeable {
public static void main(String[] args) throws IOException {
File f = File.createTempFile("chess-moves-", ".temp");
try (FieldSaver test = new FieldSaver(f);) {
for (byte i = 0; i < 100; i++) {
test.add(new Field(8, new WildBoar(i, i), new Hunter(i, i), new HuntingDog[] {
new HuntingDog(i, i),
new HuntingDog(i, i),
new HuntingDog(i, i) }));
}
// Get a few Fields by index
System.out.println(test.get(0));
System.out.println(test.get(50));
System.out.println(test.get(99));
// EOF exception, there is no Field 100
// System.out.println(test.get(100));
}
}
private final RandomAccessFile data;
public FieldSaver(File f) throws FileNotFoundException {
data = new RandomAccessFile(f, "rw");
}
public void add(Field field) throws IOException {
data.seek(data.length());
field.write(data);
}
public Field get(int index) throws IOException {
data.seek(index * Field.STORAGE_LENGTH_BYTES);
return Field.read(data);
}
public void close() throws IOException { data.close(); }
static abstract class Piece {
protected byte xPos;
protected byte yPos;
public Piece(DataInput data) throws IOException {
xPos = data.readByte();
yPos = data.readByte();
}
public Piece(byte xPos, byte yPos) {
this.xPos = xPos;
this.yPos = yPos;
}
public void write(DataOutput data) throws IOException {
data.writeByte(xPos);
data.writeByte(yPos);
}
public String toString() { return "[" + xPos + ", " + yPos + "]"; }
}
static class Hunter extends Piece {
public Hunter(byte xPos, byte yPos) { super(xPos, yPos); }
public Hunter(DataInput data) throws IOException { super(data); }
}
static class HuntingDog extends Piece {
public HuntingDog(byte xPos, byte yPos) { super(xPos, yPos); }
public HuntingDog(DataInput data) throws IOException { super(data); }
}
static class WildBoar extends Piece {
public WildBoar(byte xPos, byte yPos) { super(xPos, yPos); }
public WildBoar(DataInput data) throws IOException { super(data); }
}
static class Field {
// size of boar + hunter + 3 dogs
public static final int STORAGE_LENGTH_BYTES = 2 + 2 + (3 * 2);
private int size;
private WildBoar boar;
private Hunter hunter;
private final HuntingDog[] dogs;
public Field(int size, WildBoar wildBoar, Hunter hunter, HuntingDog[] huntingDogs) {
this.size = size;
this.boar = wildBoar;
this.hunter = hunter;
this.dogs = huntingDogs;
}
public String toString() {
String result = "Wildschwein: " + boar.toString();
for (HuntingDog dog : dogs) {
result += "; Hund: " + dog.toString();
}
return result + "; Jäger: " + hunter.toString();
}
public static Field read(DataInput data) throws IOException {
WildBoar boar = new WildBoar(data);
Hunter hunter = new Hunter(data);
HuntingDog[] dogs = new HuntingDog[3];
for (int i = 0; i < 3; i++) {
dogs[i] = new HuntingDog(data);
}
return new Field(8, boar, hunter, dogs);
}
public void write(DataOutput data) throws IOException {
boar.write(data);
hunter.write(data);
for (HuntingDog dog : dogs) {
dog.write(data);
}
}
}
}
Use a Map implementation like Cache from ehcache. This library will optimize for you so you don't have to handle writing and reading to disk and manage when to keep it in memory or on disk. You can just use it as a normal map. You probably want a map instead of a list for faster lookup so the library can optimize even more for you.
http://www.ehcache.org/
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.heap(100))
.build())
.build(true);
Cache<Long, String> preConfigured
= cacheManager.getCache("preConfigured", Long.class, String.class);

Issue loading data from csv file - Java

As it stands I have a data set in the form of a .csv file which you can find here. Also there is some brief documentation on it which you can find here. What I am attempting to do is to manipulate the data set so that I can work with some machine learning algorithms but as it stands I can't seem to print the outputted data to the console
ImageMatrix.java
import java.util.Arrays;
public class ImageMatrix {
public static int[] data;
public int classCode;
public ImageMatrix(int[] data, int classCode) {
assert data.length == 64;
}
public String toString() {
return "Class Code: " + classCode + " DataSet:" + Arrays.toString(data) + "\n";
}
public int[] getData() {
return data;
}
public int getClassCode() {
return classCode;
}
}
ImageMatrixDB.java
import java.io.*;
import java.util.*;
public class ImageMatrixDB implements Iterable<ImageMatrix> {
List<ImageMatrix> list = new ArrayList<ImageMatrix>();
public static ImageMatrixDB load(String f) throws IOException {
ImageMatrixDB result = new ImageMatrixDB();
try (FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr)) {
for (String line; null != (line = br.readLine()); ) {
int lastComma = line.lastIndexOf(',');
int classCode = Integer.parseInt(line.substring(1 + lastComma));
int[] data = Arrays.stream(line.substring(0, lastComma).split(","))
.mapToInt(Integer::parseInt)
.toArray();
result.list.add(new ImageMatrix(data, classCode));
}
System.out.println(ImageMatrix.data.toString());
}
return result;
}
public Iterator<ImageMatrix> iterator() {
return this.list.iterator();
}
public static void main(String[] args){
ImageMatrixDB i = new ImageMatrixDB();
i.load("dataset1.csv"); // <<< ERROR IS HERE
}
}
The error is within my main function on the line i.load(... I know I must be missing something or have made a mistake somewhere, I have tried altering the data from static but it just throws more errors and I can't figure it out. Any ideas?
Your issue is in the ImageMatrix class.
You never set the int[] data in the constructor. You have:
public ImageMatrix(int[] data, int classCode) {
assert data.length == 64;
}
You need:
public ImageMatrix(int[] data, int classCode) {
assert data.length == 64;
this.data = data;
this.classCode = classCode;
}
Here is your updated/complete/working code:
ImageMatrix:
import java.util.*;
public class ImageMatrix {
private int[] data;
private int classCode;
public ImageMatrix(int[] data, int classCode) {
assert data.length == 64;
this.data = data;
this.classCode = classCode;
}
public String toString() {
return "Class Code: " + classCode + " DataSet:" + Arrays.toString(data) + "\n";
}
public int[] getData() {
return data;
}
public int getClassCode() {
return classCode;
}
}
ImageMatrixDB:
import java.util.*;
import java.io.*;
public class ImageMatrixDB implements Iterable<ImageMatrix> {
private List<ImageMatrix> list = new ArrayList<ImageMatrix>();
public ImageMatrixDB load(String f) throws IOException {
try (
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr)) {
String line = null;
while((line = br.readLine()) != null) {
int lastComma = line.lastIndexOf(',');
int classCode = Integer.parseInt(line.substring(1 + lastComma));
int[] data = Arrays.stream(line.substring(0, lastComma).split(","))
.mapToInt(Integer::parseInt)
.toArray();
ImageMatrix matrix = new ImageMatrix(data, classCode);
list.add(matrix);
}
}
return this;
}
public void printResults(){
for(ImageMatrix matrix: list){
System.out.println(matrix);
}
}
public Iterator<ImageMatrix> iterator() {
return this.list.iterator();
}
public static void main(String[] args){
ImageMatrixDB i = new ImageMatrixDB();
try{
i.load("cw2DataSet1.csv");
i.printResults();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
Your load method can throw an IOException. You need to catch it in order to successfully compile
public static void main(String[] args){
ImageMatrixDB i = new ImageMatrixDB();
try{
i.load("dataset1.csv"); // <<< ERROR IS HERE
}
catch(Exception e){
System.out.println(e.getMessage());
}
}

Found some code for serialization of an ItemStack in Bukkit, how to deserialize it?

I've dug up the following code for serializing an ItemStack in bukkit (Minecraft). I've been able to serialize an item in hand with the following:
itemString = ItemStackUtils.deserialize(player.getInventory().getItemInHand());
I can't figure out how to utilize the deserial call however. What I am trying to do is to pull an item from the players hand, serialize it, stick it into a config file, then when the player runs another command... deserialize it and slap it into their inventory. I am fairly certain this class will meet my needs if I just can get the last part working.
public final class ItemStackUtils {
public static String getEnchants(ItemStack i){
List<String> e = new ArrayList<String>();
Map<Enchantment, Integer> en = i.getEnchantments();
for(Enchantment t : en.keySet()) {
e.add(t.getName() + ":" + en.get(t));
}
return StringUtils.join(e, ",");
}
public static String deserialize(ItemStack i){
String[] parts = new String[6];
parts[0] = i.getType().name();
parts[1] = Integer.toString(i.getAmount());
parts[2] = String.valueOf(i.getDurability());
parts[3] = i.getItemMeta().getDisplayName();
parts[4] = String.valueOf(i.getData().getData());
parts[5] = getEnchants(i);
return StringUtils.join(parts, ";");
}
public ItemStack deserial(String p){
String[] a = p.split(";");
ItemStack i = new ItemStack(Material.getMaterial(a[0]), Integer.parseInt(a[1]));
i.setDurability((short) Integer.parseInt(a[2]));
ItemMeta meta = i.getItemMeta();
meta.setDisplayName(a[3]);
i.setItemMeta(meta);
MaterialData data = i.getData();
data.setData((byte) Integer.parseInt(a[4]));
i.setData(data);
if (a.length > 5) {
String[] parts = a[5].split(",");
for (String s : parts) {
String label = s.split(":")[0];
String amplifier = s.split(":")[1];
Enchantment type = Enchantment.getByName(label);
if (type == null)
continue;
int f;
try {
f = Integer.parseInt(amplifier);
} catch(Exception ex) {
continue;
}
i.addEnchantment(type, f);
}
}
return i;
}
}
here it saves & loads the whole NBT
package XXXXXXXXX
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import net.minecraft.server.v1_8_R3.MojangsonParseException;
import net.minecraft.server.v1_8_R3.MojangsonParser;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
public class ItemSerialize {
public ItemSerialize() {
}
public static String serialize(ItemStack i) {
String[] parts = new String[7];
parts[0] = i.getType().name();
parts[1] = Integer.toString(i.getAmount());
parts[2] = String.valueOf(i.getDurability());
parts[3] = i.getItemMeta().getDisplayName();
parts[4] = String.valueOf(i.getData().getData());
parts[5] = getEnchants(i);
parts[6] = getNBT(i);
return StringUtils.join(parts, ";");
}
public static String getEnchants(ItemStack i) {
List<String> e = new ArrayList<String>();
Map<Enchantment, Integer> en = i.getEnchantments();
for (Enchantment t : en.keySet()) {
e.add(t.getName() + ":" + en.get(t));
}
return StringUtils.join(e, ",");
}
public static String getLore(ItemStack i) {
List<String> e = i.getItemMeta().getLore();
return StringUtils.join(e, ",");
}
public static String getNBT(ItemStack i) {
net.minecraft.server.v1_8_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(i);
NBTTagCompound compound = nmsStack.hasTag() ? nmsStack.getTag() : new NBTTagCompound();
return compound.toString();
}
public static ItemStack setNBT(ItemStack i, String NBT) {
net.minecraft.server.v1_8_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(i);
try {
NBTTagCompound compound = MojangsonParser.parse(NBT);
nmsStack.setTag(compound);
} catch (MojangsonParseException e1) {
e1.printStackTrace();
}
return CraftItemStack.asBukkitCopy(nmsStack);
}
#SuppressWarnings("deprecation")
public static ItemStack deserial(String p) {
String[] a = p.split(";");
ItemStack i = new ItemStack(Material.getMaterial(a[0]), Integer.parseInt(a[1]));
i.setDurability((short) Integer.parseInt(a[2]));
ItemMeta meta = i.getItemMeta();
meta.setDisplayName(a[3]);
i.setItemMeta(meta);
MaterialData data = i.getData();
data.setData((byte) Integer.parseInt(a[4]));
i.setData(data);
if (!a[6].isEmpty()) {
i = setNBT(i, a[6]);
}
if (!a[5].isEmpty()) {
String[] parts = a[5].split(",");
for (String s : parts) {
String label = s.split(":")[0];
String amplifier = s.split(":")[1];
Enchantment type = Enchantment.getByName(label);
if (type == null)
continue;
int f;
try {
f = Integer.parseInt(amplifier);
} catch (Exception ex) {
continue;
}
i.addUnsafeEnchantment(type, f);
}
}
return i;
}
}
ItemStack rItem = ItemStackUtils.deserial(itemString);

After reading xml file trying to send mail

I am trying to read the xml file and trying to send the mail.
here is the xml code
<?xml version="1.0" encoding="UTF-8"?><xml>
<u_incident_task>
<description>messafe</description>
<priority>1</priority>
<number>12345</number>
<u_task_incident_service_ci>A</u_task_incident_service_ci>
</u_incident_task>
<u_incident_task>
<description>messafe</description>
<priority>3</priority>
<number>123456</number>
<u_task_incident_service_ci>A</u_task_incident_service_ci>
</u_incident_task>
</xml>
so when ever Priority is 1 i need to send mail. currently my below code is working and able to send.
But when ever Priority is updated from 3 to 1 mail is not able to send. may be the reason i am storing all the number in array since i dont want to send mail repeatedly. so i stored the numbers in array so that i can be sent only once. so while i am storing the numbers if any Priority is updated to 1 mail is not working.
But when record is created with 1 mail i sent but if we updated the existing priority from 3 to 1 it not workin. complete working code is here. can any one help what i missed in logic pls?
complete code here.
Incidentreport2000.java
package com.asn.model;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.asn.model.incident200;
import com.asn.model.incident1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.mail.Message;
import javax.mail.MessagingException;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
public class incidentreport2000 implements Runnable {
List<String> number1 = new ArrayList<String>();
public incidentreport2000(){
}
public void run(){
for(int i=1;i>0;i++){
try{
Thread.sleep(9600);
List<incident1> incident1List = new ArrayList<incident1>();
List<incident200> incident200List = new ArrayList<incident200>();
FileWriter fw =null;
BufferedWriter bw=null;
String Path = "C:\\Users";
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
InputStream input = null;
String address ="C:\\Users" ;
String localFileName = "\\file.xml";
try {
File fXmlFile = new File(Path + "\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList incident1NodeList = doc.getElementsByTagName("u_incident_task");
for (int temp = 0; temp < incident1NodeList.getLength(); temp++) {
Node varNode = incident1NodeList.item(temp);
if (varNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) varNode;
NodeList u_task_incident_service_ciList = eElement.getElementsByTagName
("u_task_incident_service_ci");
NodeList varsionList = eElement.getElementsByTagName("priority");
NodeList numberList = eElement.getElementsByTagName("number");
NodeList descriptionList = eElement.getElementsByTagName("description");
/* Node u_task_incident_service_ciNode =
u_task_incident_service_ciList.item(0);
Node priorityNode = varsionList.item(0);*/
Node u_task_incident_service_ciNode =
u_task_incident_service_ciList.item(0);
Node priorityNode = varsionList.item(0);
Node numberNode = numberList.item(0);
Node descriptionNode = descriptionList.item(0);
if (u_task_incident_service_ciNode.getNodeType() == Node.ELEMENT_NODE
&& priorityNode.getNodeType() ==
Node.ELEMENT_NODE) {
Element u_task_incident_service_ciElement = (Element)
u_task_incident_service_ciNode;
Element priorityElement = (Element) priorityNode;
Element numberElement = (Element) numberNode;
Element descriptionElement = (Element) descriptionNode;
incident1 incident1 = new incident1(
u_task_incident_service_ciElement.getTextContent(),
priorityElement.getTextContent());
incident1List.add(incident1);
incident200 incident200 = new incident200(
u_task_incident_service_ciElement.getTextContent(),
priorityElement.getTextContent(),numberElement.getTextContent (),descriptionElement.getTextContent());
incident200List.add(incident200);
}
}
// fw = new FileWriter(file.getAbsoluteFile());
// bw = new BufferedWriter(fw);
}
} catch (Exception e) {
e.printStackTrace();
}
Map<incident1, Integer> count = new HashMap<incident1, Integer>();
for (incident1 c : incident1List)
if (!count.containsKey(c))
count.put(c, Collections.frequency(incident1List, c));
List<String> prioritys = getprioritys(count);
List<String> u_task_incident_service_cis = getu_task_incident_service_cis(count);
Map<incident200, Integer> count1 = new HashMap<incident200, Integer>();
for (incident200 c1 : incident200List)
if (!count1.containsKey(c1))
count1.put(c1, Collections.frequency(incident200List, c1));
List<String> number = getnumber(count1);
List<String> description = getdescription(count1);
List<String> prioritys1 = getprioritys1(count1);
List<String> u_task_incident_service_cis1 = getu_task_incident_service_cis1(count1);
for (String v2 : prioritys1 ) {
if (v2 =="P1" ){
for (String n2 : number ) {
System.out.println(number);
if (!number1.contains(n2)) {
for (String d : description){
for (String m3 : u_task_incident_service_cis1){
if (m3 == "A"){
getCountu_task_incident_service_cis1( m3,prioritys1,n2,d, count1);
}
if (m3 == "B"){
getCountu_task_incident_service_cis1( m3,prioritys1,n2,d, count1);
}
}} number1.add(n2);
}}
}}
}catch(Exception e){}
}
}
private static List<String> getu_task_incident_service_cis(Map<incident1, Integer>
count) {
List<String> u_task_incident_service_cis = new ArrayList<String>();
for (Map.Entry<incident1, Integer> ent : count.entrySet())
if (!u_task_incident_service_cis.contains(ent.getKey
().getu_task_incident_service_ci()))
u_task_incident_service_cis.add(ent.getKey().getu_task_incident_service_ci
());
System.out.println(u_task_incident_service_cis);
if(!u_task_incident_service_cis.contains("A"))
u_task_incident_service_cis.add("A");
return u_task_incident_service_cis;
}
private static List<String> getu_task_incident_service_cis1(Map<incident200, Integer>
count) {
List<String> u_task_incident_service_cis1 = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!u_task_incident_service_cis1.contains(ent.getKey
().getu_task_incident_service_ci()))
u_task_incident_service_cis1.add(ent.getKey().getu_task_incident_service_ci
());
System.out.println(u_task_incident_service_cis1+"NewCIS");
if(!u_task_incident_service_cis1.contains("BIRSD"))
u_task_incident_service_cis1.add("BIRSD");
return u_task_incident_service_cis1;
}
private static String getNumOfu_task_incident_service_cis(String
u_task_incident_service_ci, List<String> prioritys,
Map<incident1, Integer>
count) {
StringBuilder builder = new StringBuilder();
for (String v : prioritys) {
Integer cnt = count.get(new incident1(u_task_incident_service_ci, v));
if (cnt == null) {
cnt = 0;
}
}
return builder.toString();
}
private static String getCountu_task_incident_service_cis1(String
u_task_incident_service_ci, List<String> prioritys1, String number,String description,
Map<incident200, Integer>
count1) {
StringBuilder builder1 = new StringBuilder();
List<String> ARRAY = new ArrayList<>();
for (String v : prioritys1) {
if ( v == "P1") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
}
else
if (cnt1 !=0){
cnt1 = 1;
if (!ARRAY.contains(number)) {
mail1 (u_task_incident_service_ci,v,number,description);
ARRAY.add(number);
}
}}
else
if ( v == "P2") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
}
else
if (cnt1 !=0){
if (!ARRAY.contains(number)) {
mail1(u_task_incident_service_ci,v,number,description);
ARRAY.add(number);
}}
}}
return number;
}
private static List<String> getprioritys(Map<incident1, Integer> count) {
List<String> prioritys = new ArrayList<String>();
for (Map.Entry<incident1, Integer> ent : count.entrySet())
if (!prioritys.contains(ent.getKey().getpriority()))
prioritys.add(ent.getKey().getpriority());
Collections.sort(prioritys);
return prioritys;
}
private static List<String> getprioritys1(Map<incident200, Integer> count) {
List<String> prioritys1 = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!prioritys1.contains(ent.getKey().getpriority()))
prioritys1.add(ent.getKey().getpriority());
;
//Collections.sort(prioritys);
System.out.println("check1");
return prioritys1;
}
private static List<String> getnumber(Map<incident200, Integer> count) {
List<String> number = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!number.contains(ent.getKey().getnumber()))
number.add(ent.getKey().getnumber());
return number;
}
private static List<String> getdescription(Map<incident200, Integer> count) {
List<String> description = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!description.contains(ent.getKey().getdescription()))
description.add(ent.getKey().getdescription());
return description;
}
public static void mail1(String Email,String v,String n, String d) {
System.out.println(Email);
System.out.println(v);
final String username = "mailid";
final String password = "password";
//w2011.kpnnl.local
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
//props.put("mail.transport.protocol","smtp");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse
("toaddress"));
message.setSubject("Priority:"+v+": "+n+"Incident has bee raised for the team"+Email);
message.setText("messager,"
);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
incidentreport2000 mrt = new incidentreport2000();
Thread t = new Thread(mrt);
t.start();
String Path = "C:\\Users";
List<String> number1 = new ArrayList<String>();
}
}
incident200.java
package com.asn.model;
public class incident200 {
private String u_task_incident_service_ci;
private String priority;
private String number;
private String description;
public incident200(String u_task_incident_service_ci, String priority, String number,String description ) {
super();
if (u_task_incident_service_ci.equals("A"))
{ String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
if (priority.equals("1"))
{ String priority1 = "P1";
this.priority = priority1;}
else
if (priority.equals("3"))
{ String priority3 = "P3";
this.priority = priority3;}
else
if (priority.equals("4"))
{ String priority4 = "P3";
this.priority = priority4;}
else
if (priority.equals("5"))
{ String priority5 = "P3";
this.priority = priority5;}
else
if (priority.equals("2"))
{ String priority2 = "P2";
this.priority = priority2;}
else
this.priority = priority;
this.number = number;
this.description = description;
}
public String getu_task_incident_service_ci() {
return u_task_incident_service_ci;
}
public void setu_task_incident_service_ci(String u_task_incident_service_ci) {
this.u_task_incident_service_ci = u_task_incident_service_ci;
}
public String getpriority() {
return priority;
}
public void setpriority(String priority) {
this.priority = priority;
}
public String getnumber() {
return number;
}
public void setnumber(String number) {
this.number = number;
}
public String getdescription() {
return description;
}
public void setdescription(String description) {
this.description = description;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((u_task_incident_service_ci == null) ? 0 : u_task_incident_service_ci.hashCode());
result = prime * result + ((priority == null) ? 0 : priority.hashCode());
result = prime * result + ((number == null) ? 0 : number.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
incident200 other = (incident200) obj;
if (u_task_incident_service_ci == null) {
if (other.u_task_incident_service_ci != null)
return false;
} else if (!u_task_incident_service_ci.equals(other.u_task_incident_service_ci))
return false;
if (priority == null) {
if (other.priority != null)
return false;
} else if (!priority.equals(other.priority))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
return true;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("incident99 [u_task_incident_service_ci=");
builder.append(u_task_incident_service_ci);
builder.append(", priority1=");
builder.append(priority);
builder.append(", number=");
builder.append(number);
builder.append(", description=");
builder.append(description);
builder.append("]");
return builder.toString();
}
}
Incident1.java
package com.asn.model;
public class incident1 {
private String u_task_incident_service_ci;
private String priority;
public incident1(String u_task_incident_service_ci, String priority) {
super();
if (u_task_incident_service_ci.equals("A"))
{ String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
if (priority.equals("1"))
{ String priority1 = "P1";
this.priority = priority1;}
else
if (priority.equals("3"))
{ String priority3 = "P3";
this.priority = priority3;}
else
this.priority = priority;
}
public String getu_task_incident_service_ci() {
return u_task_incident_service_ci;
}
public void setu_task_incident_service_ci(String u_task_incident_service_ci) {
this.u_task_incident_service_ci = u_task_incident_service_ci;
}
public String getpriority() {
return priority;
}
public void setpriority(String priority) {
this.priority = priority;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((u_task_incident_service_ci == null) ? 0 : u_task_incident_service_ci.hashCode());
result = prime * result + ((priority == null) ? 0 : priority.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
incident1 other = (incident1) obj;
if (u_task_incident_service_ci == null) {
if (other.u_task_incident_service_ci != null)
return false;
} else if (!u_task_incident_service_ci.equals(other.u_task_incident_service_ci))
return false;
if (priority == null) {
if (other.priority != null)
return false;
} else if (!priority.equals(other.priority))
return false;
return true;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("incident1 [u_task_incident_service_ci=");
builder.append(u_task_incident_service_ci);
builder.append(", priority=");
builder.append(priority);
builder.append("]");
return builder.toString();
}
}
This code is rather ... peculiar. Looking at one method:
private static String
getNumOfu_task_incident_service_cis(String u_task_incident_service_ci,
List<String> prioritys,
Map<incident1, Integer> count) {
StringBuilder builder = new StringBuilder();
for (String v : prioritys) {
Integer cnt = count.get(new incident1(u_task_incident_service_ci, v));
if (cnt == null) {
cnt = 0;
}
}
return builder.toString();
}
Several "count" values are obtained from the Map, but the StringBuilder happily remains unchanged; the method always returns "".
Another method:
private static String
getCountu_task_incident_service_cis1( ..., String number,... ){
//...
if ( v == "P1") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
} else
if (cnt1 != 0){
cnt1 = 1;
//...
return number;
}
Here, cnt1 is set to 0 or 1, but not used later on within the same if statement. And the return value is number, a String parameter. So - what's the point?
public incident200(String u_task_incident_service_ci,
String priority, String number,String description ) {
super();
if (u_task_incident_service_ci.equals("A")){
String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
What's the purpose of this complication? A single statement would be sufficient:
this.u_task_incident_service_ci = u_task_incident_service_ci;
I regret that I can only point out why this code may be difficult to understand. Cleaning up the code might be a good start towards finding the bug.
Later
If method getCountu_task_incident_service_cis1 should send emails, it does so for priorities P1 and P2 only:
for (String v : prioritys1) {
if ( v == "P1") {
// ...
} else
if ( v == "P2") {
//...
}
}
Moreover, method run contains
for (String v2 : prioritys1 )
if (v2 =="P1" ){
// some code...
}
}
Do you expect that any other priority is ever sent?

Categories

Resources