I am attempting to take all the information that added by the user in the text fields, and then add them a 2D array. I have accomplish that by doing this:
int minorinput = JOptionPane.showConfirmDialog(frame, panel1, "Choose Minor Stats", JOptionPane.OK_CANCEL_OPTION);
if(minorinput == JOptionPane.OK_OPTION)
{
int [][] pureMR = new int [4][4];
pureMR[0][0] = Integer.parseInt(Melee.getText());
pureMR[1][0] = Integer.parseInt(Ranged.getText());
pureMR[2][0] = Integer.parseInt(RC.getText());
pureMR[3][0] = Integer.parseInt(Negotiation.getText());
pureMR[0][1] = Integer.parseInt(Dodge.getText());
pureMR[1][1]= Integer.parseInt(Perception.getText());
pureMR[2][1] = Integer.parseInt(Will.getText());
pureMR[3][1] = Integer.parseInt(Procure.getText());
pureMR[0][2] = Integer.parseInt(rideBox.getText());
pureMR[1][2] = Integer.parseInt(rideBox2.getText());
pureMR[2][2] = Integer.parseInt(artBox.getText());
pureMR[3][2] = Integer.parseInt(art2.getText());
pureMR[0][3] = Integer.parseInt(knowledgeBox.getText());
pureMR[1][3] = Integer.parseInt(knowledge2Box.getText());
pureMR[2][3] = Integer.parseInt(infoBox.getText());
pureMR[3][3] = Integer.parseInt(info2Box.getText());
But I can't figure out how to transfer the array to a different class, where it will be used in an object constructor that will allow me to print via the objects toString method.
How should I be going about transferring the 2D array from class to class?
Class I want to add the Array to:
if(pureinput == JOptionPane.OK_OPTION)
{
String name = nameBox.getText();
String age = ageBox.getText();
String gender = genderBox.getText();
String bloodType = bloodBox.getText();
String height = heightBox.getText();
String weight = weightBox.getText();
String zodiac= zodiacBox.getText();
String work = workBox.getText();
String cover = coverBox.getText();
String breed = "Pure";
String sydrome = syndrome1Box.getText();
int[] pureBS = new int[4];
pureBS[0] = Integer.parseInt(bodyBox.getText());
pureBS[1] = Integer.parseInt(senseBox.getText());
pureBS[2] = Integer.parseInt(mindBox.getText());
pureBS[3] = Integer.parseInt(skillBox.getText());
int[] pureSS = new int[6];
pureSS[0] = Integer.parseInt(mHPBox.getText());
pureSS[1] = Integer.parseInt(stockBox.getText());
pureSS[2] = Integer.parseInt(savingsBox.getText());
pureSS[3] = Integer.parseInt(initBox.getText());
pureSS[4] = Integer.parseInt(moveBox.getText());
pureSS[5] = Integer.parseInt(dashBox.getText());
String origin = orginBox.getText();
String exp = ExperienceBox.getText();
String encounter = EncounterBox.getText();
String awake = AwakeningBox.getText();
int eRate = Integer.parseInt(EncroachmentRateBox.getText());
String impulse = ImpulseBox.getText();
int eRate2= Integer.parseInt(EncroachmentRateBox2.getText());
Character pure = new Character(name,age,bloodType,gender, height,weight,zodiac,work,cover,
breed,sydrome,pureBS, pureSS,origin,exp,encounter,awake,eRate,
impulse,eRate2, //Needs a 2D array here);
System.out.println(pure.pureToString());
It creates this dialog box.
The simplest way: simply call a setter method, and pass the array into the instance:
if(minorinput == JOptionPane.OK_OPTION) {
int [][] pureMR = new int [4][4];
// code to fill array
// assuming your OtherClass has this type of setter method...
// because if it doesn't, it NEEDS one.
otherInstance.setPureMr(pureMR);
Where you get your reference to the otherInstance instance will depend totally on how your program is structured, information that we are not privy to at this moment.
Having said this, I do have to wonder if your 2D array is a kludge, if you're far better off creating a class to hold this information, and then passing an object of this class into your other instance.
Related
currently, I'm doing an assignment that deals with the ArrayList class.
at some point, I need to check of the id of the instructor and make sure that the instructor is not added twice to the ArrayList, so I made a for loop to go through all the id that has been registered and get the id and check if it exists already
the problem is when I use the method " .size()" in the loop, the JVM throws NullPointerException
and I don't know why.
==========================================================================
what I need to read is this:
\\name - id - dateOfBirth - gender - degree - speciality - city - availability
Amanda Smith, 102020, 320101200000, M, PhD, Software Engineering, NewYork, true
=======================================================================
this is the code:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) { //ERROR OCCURE
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main
The problem is membersList doesn't exist when you call .size() on it
instead of
ArrayList<UniversityMember> membersList;
you need to initialize it
ArrayList<UniversityMember> membersList = new ArrayList<UniversityMember>();
You need to initialize the ArrayList.
Like that ArrayList membersList = new ArrayList();
After that, in the first size() returns 0 and not null. Remember all data structure must be initialize in java.
You haven't added anything to the membersList then asking for the size for something that has nothing in it.
Example of whats going on
String str;
for(int i = 0; i < str.length(); i++){
System.out.println("hey");
}
also you need to declare the array list like this
ArrayList<Method name> membersList = new ArrayList<Method name>();
also don't forget to import the ArrayList class
import java.util.ArrayList;
nvm I figured out that I haven't initialized my array ( ╥ω╥ )
I'll keep the question for others to be carefull
==================================================
The code after fixing it:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
/* ===== FIXING THE ERROR ======*/
membersList = new ArrayList();
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) {
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main
Hi.
I'm making an app that receives data from bluetooth by using stringbuilder
And makes it slice for using another activity.
The image shows what i want to make.
Q1. What should i use c->d, d->e ?
Q2. There will be a lot of data, I want to know the way to simplify this sequence
******************** edited ********************
I have practiced by adding value to Arraylist.
But in String Array, there is no .get(), so i couldn't access to element's length.
public static ArrayList<String> randomValue = new ArrayList<>();
public static int iDistance=0, xIAngle=0, yIAngle=0, zIAngle=0;
public static String distance, xAngle, yAngle, zAngle;
randomValue.add("12345090080070");
randomValue.add("15640080085071");
randomValue.add("16542070084074");
randomValue.add("12645080087078");
randomValue.add("21345084081060");
randomValue.add("14785078075065");
randomValue.add("13155079077077");
randomValue.add("14623080078078");
randomValue.add("14918086080078");
randomValue.add("15684085082080");
for (int i=0; i<randomValue.size(); i++){
String a = randomValue.get(i);
String distance = a.substring(0,5);
String xAngle = a.substring(5,8);
String yAngle = a.substring(8,11);
String zAngle = a.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
It seems like you are just stuck on finding the equivalent of get for a string array. To access an element in an array, the syntax is array[I], so if you were using a string array, this line:
String a = randomValue.get(i);
would have been:
String a = randomValue[i];
The code for your sequence of transformations can be shortened with Streams:
// this is the sequence of transformation starting with the sting builder "a"
List<String> randomValueWithLength14 =
Arrays.stream(a.toString().split(";")).filter(x -> x.length() == 14)
.collect(Collectors.toList());
// this is the for loop shown in your code
for (int i=0; i<randomValueWithLength14.size(); i++){
String s = randomValueWithLength14.get(i);
String distance = a.substring(0,5);
String xAngle = s.substring(5,8);
String yAngle = s.substring(8,11);
String zAngle = s.substring(11,14);
//String to int
iDistance = Integer.parseInt(distance);
xIAngle = Integer.parseInt(xAngle);
yIAngle = Integer.parseInt(yAngle);
zIAngle = Integer.parseInt(zAngle);
}
I am having issues understanding how to create an array of n objects in Java.
This is the constructor of class ServicePath as follows:
public ServicePath(String id) {
this.id = id;
}
This is the elements of the array that I would like to create the objects.
String ServicePathArrays[] = {"SH11","SH13","SH17","SH110","SH111","SH112","SH115", ...}
I tried the following, but it creates it manually.
ServicePath[] servicePathArray = new ServicePath[ServicePathArrays.length];
For example, manually it creates the following
ServicePath[0] = new ServicePath("SH11");
ServicePath[1] = new ServicePath("SH13");
..
..
I would like to create it automatically using
String ServicePathArrays in such way:
ServicePath[0].id = "SH11";
ServicePath[1].id = "SH12";
ServicePath[2].id = "SH13";
..
..
This could be done using the functional behavior of jdk8+ :
String servicePathArray[] = {"SH11", "SH13", "SH17",
"SH110", "SH111", "SH112", "SH115"};
List<ServicePath> collection = Stream.of(servicePathArray)
.map(ServicePath::new)
.collect(Collectors.toList());
System.out.println(collection);
String ServicePathArrays[] = {"SH11","SH13","SH17","SH110","SH111","SH112","SH115", ...};
ServicePath[] servicePathArray = new ServicePath[ServicePathArrays.length];
for(int i = 0; i < ServicePathArrays.length; i++) {
servicePathArray [i] = new ServicePath(ServicePathArrays[i]);
}
I am naming all of these one by one. Is there a method that takes less space?
public class Matt{
PImage matt,
imgLS1, imgLS2, imgLS3, imgRS1, imgRS2, imgRS3,
imgLSB1, imgLSB2, imgLSB3, imgRSB1, imgRSB2, imgRSB3,
imgLW1, imgLW2, imgLW3, imgRW1, imgRW2, imgRW3,
imgLWB1, imgLWB2, imgLWB3, imgRWB1, imgRWB2, imgRWB3;
public Matt(){
imgLS1 = loadImage("./Images/Matt/MattLS1.png");
imgLS2 = loadImage("./Images/Matt/MattLS2.png");
imgLS3 = loadImage("./Images/Matt/MattLS3.png");
imgRS1 = loadImage("./Images/Matt/MattRS1.png");
imgRS2 = loadImage("./Images/Matt/MattRS2.png");
imgRS3 = loadImage("./Images/Matt/MattRS3.png");
imgLSB1 = loadImage("./Images/Matt/MattLSB1.png");
imgLSB2 = loadImage("./Images/Matt/MattLSB2.png");
imgLSB3 = loadImage("./Images/Matt/MattLSB3.png");
imgRSB1 = loadImage("./Images/Matt/MattRSB1.png");
imgRSB2 = loadImage("./Images/Matt/MattRSB2.png");
imgRSB3 = loadImage("./Images/Matt/MattRSB3.png");
imgLW1 = loadImage("./Images/Matt/MattLW1.png");
imgLW2 = loadImage("./Images/Matt/MattLW2.png");
imgLW3 = loadImage("./Images/Matt/MattLW3.png");
imgRW1 = loadImage("./Images/Matt/MattRW1.png");
imgRW2 = loadImage("./Images/Matt/MattRW2.png");
imgRW3 = loadImage("./Images/Matt/MattRW3.png");
imgLWB1 = loadImage("./Images/Matt/MattLWB1.png");
imgLWB2 = loadImage("./Images/Matt/MattLWB2.png");
imgLWB3 = loadImage("./Images/Matt/MattLWB3.png");
imgRWB1 = loadImage("./Images/Matt/MattRWB1.png");
imgRWB2 = loadImage("./Images/Matt/MattRWB2.png");
imgRWB3 = loadImage("./Images/Matt/MattRWB3.png");
}
}
Put your images in a Map<String,PImage>, organizing the map by image suffix. As far as accessing the images is concerned, this approach may be slightly less convenient/efficient than using variables directly, but it will save you a lot of space:
static final String[] suffixes = new String[] {"LS1", "LS2", "LS3", ..., "RWB3"};
Map<String,PImage> images = new HashMap<String,PImage>();
public Matt() {
for (String suffix : suffixes) {
PImage image = loadImage("./Images/Matt/Matt"+suffix+".png");
images.put(suffix, image);
}
}
Since the "LS", etc., seem to have semantic meaning, I'd suggest a variation of the solution by #dasblinkenlight that uses an enum:
final int N_FILES = 3; // files/position -- could also be a variable
enum Position {
LS, RS, LSB, RSB, LW, RW, LWB, LRB
}
Map<Position, String[]> files = new EnumMap<>(Position.class);
for (Position pos : Position.values()) {
String[] posFiles = new String[N_FILES];
files.put(pos, posFiles);
for (int i = 1; i <= N_FILES; ++i) {
posFiles[i-1] = "./Images/Matt/Matt" + pos.name() + i + ".png";
}
}
Then you can access any element with code like this:
Position p = RS; // or any other value
int index = 0; // 0..(N_FILES-1), corresponding to suffixes 1..N_FILES
String fileName = files.get(p)[i];
In my program the user declares a string of numbers that I am trying to figure out to turn into an array.
Example:
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, "27, 26,
28");
Im trying to figure out how to add that string into my class instance variable.
This is what I have:
private String sampleID;
private String weekOfTest;
private int engineerID;
private String[] strengths = new String[20];
private static int count;
public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String strengths)
{
this.sampleID = sampleID;
this.weekOfTest = weekOfTest;
this.engineerID = engineerID;
this.strengths = strengths;
count++;
}
My compile error message says incompatible types, required: String[], found: String
It is because you have declared String[] strengths which is an array.
declare your constructor like this :
public WeeklyFiber(String sampleID, String weekOfTest, int engineerID, String[] strengths)
{
this.sampleID = sampleID;
this.weekOfTest = weekOfTest;
this.engineerID = engineerID;
this.strengths = strengths;
count++;
}
Make a call like :
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32, new String[] {"27","26", "28"});
You need to parse that String of numbers to multiple Strings. For example,
this.strengths = strengths.split(",");
You can't say this.strengths = strengths because the strengths argument is of type String and not String[]. That is where your error is coming from.
Pass it like this:
WeeklyFiber week2 = new WeeklyFiber("CS4567", "11/24/13", 32,
new String[] { "27", "26", "28" });