storing passed variables into array automatically - java - java

I am having trouble storing a variable passed from a class into another classe's array.
I am passing a double that has been scanned in class A, to class B where I wish for the doubles to be stored in a double array, as long as the scanner in class A hasNext().
My code in class B, resembles something like this:
// I can't seem to get the passed doubles to be stored as individual elements of the array
public class B {
public final static int MAX_SIZE = 200;
public int i;
public double passedOne;
public void store() {
double[] storedOneVars = storedOneVars[MAX_SIZE]; // create a system to store variables in the array
for (i = 0; i < MAX_SIZE; i++) {
storedOneVars[i] = passedOne;
}
for (double s : storedOneVars) {
System.out.println(s);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new NumberRow().store();
}
}
I am open to suggestions :D

My Java is a little rusty, but I don't see where you are passing variable references to your class B. To add a reference to a value you can either create a constructor which excepts a parameter or pass the parameter into your store() method.
Also, you are instantiating your array incorrectly.
double[] storedOneVars = storedOneVars[MAX_SIZE];
should be
double[] storedOneVars = new double[MAX_SIZE];
You are also instantiating NumberRow but not assigning to a reference variable. Even worse is there is no NumberRow class. There is a class B. so is should be something like this:
B myB = new B();
Here is an example:
class B {
private double[] myDoubleArray;
public double[] getMyDoubleArray() {
return myDoubleArray;
}
public void setMyDoubleArray(double[] myDoubleArray) {
this.myDoubleArray = myDoubleArray;
}
public B(double[] dArray){
setMyDoubleArray(dArray);
}
public void store() {
for (double s : getMyDoubleArray()) {
System.out.println(s);
}
}
}
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] myd = new double[]{1,2,3};
B myB = new B(myd);
myB.store();
}
}

Related

How can i call the method from another class?

Hi i am trying to solve the problem I am facing
public class exam {
public static void main(String[] args) {
test1 a = new test1();
}
int zahl(int x, int y) {
int e;
if(x>y) {
e=x-y;
}else {
e=y-x;
}
if(e==0) {
return 0;
}
int z=0;
int i=1;
while(i<=e) {
z=z+i;
i++;
}
return z;
}
}
what I want to do is to call the zahl method to the test1 class
public class test1{
private exam b;
public void init() {
b = new exam();
}
void test() {
int result = b.zahl(2, 2);
assertEquals(1, result);
}
}
this is what I have tried, but it returns nothing, even though it's supposed to show me error.
You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.
For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.

Call method from another file as string in Java

I have file ClassifierModule.java with following method:
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {
public int measureRatio(double[] means) {
return (int) (means[3] / means[1]) ;
}
}
I'm trying to call this method to create a String in another .java file like this:
public static void main(String[] args) {
int r = ClassifierModule.measureRatio(double[]);
}
The only result I get is an error:
error: '.class' expected:
int r = ClassifierModule.measureRatio(double[]);
^
What am I doing wrong?
Here is the fuul code of ClassifierModule.java -> https://drive.google.com/file/d/1M6UlRkGEduBxQIsuOz93HEMGPtI8NiB9/view?usp=sharing
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {
public int measureRatio(double[] means){
return (int) (means[3] / means[1]) ;
}
}
measureRatio is an instance method, so it can't be called through the class, but must be called through an instance of the class.
public static void main(String[] args) {
int r = ClassifierModule.measureRatio(double[]);
}
double[] is the type which you have to pass, but it's not a value the method can work with. Change it to something like:
public static void main(String[] args) {
double[] param = new double[5];
param[0] = 7; param[1] = 8; param[2] = 4;
param[3] = 3; param[4] = 4;
ClassifierModule module = new ClassifierModule();
int r = module.measureRatio(param);
}
As not mentioned, one could simply make the method static as it does not depend on any state, on any instance object of the class.
public static int measureRatio(double[] means){
return (int) (means[3] / means[1]) ;
}
However the class extends and implements things, probably providing some context for evaluation.
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {
public ClassifierModule (ReactApplicationContext reactContext) {
super(reactContext);
}
public int measureRatio(double[] means) {
// Maybe use: getReactApplicationContext()
return (int) (means[3] / means[1]) ;
}
}
Then one would need to do something like:
int r = new ClassifierModule(...).measureRatio(double[]);
You need to do simple modifications
Non static methods can not be called by class name.
BufferListener {
public static int measureRatio(double[] means) {
return (int) (means[3] / means[1]) ;
}
}
You are trying to pass a type in second code. Not a double array.
public static void main(String[] args) {
double[] array= new double[5];
array[0] = 1; array[1] = 2; array[2] = 3;
array[3] = 5; array[4] = 4;
int r = ClassifierModule.measureRatio(array);//array is a double array
}
You are calling the method like it was a method at class level.
You have to create an object of type ClassifierModule in order to be able to call the method, like this:
public static void main(String[] args) {
// create an object
ClassifierModule cm = new ClassifierModule();
// define a parameter to be passed
double[] values = {3.0, 4.0}; // this is just an example array!
// and call the method with that parameter
int r = cm.measureRatio(double[]);
}
As an alternative, you could make the method of the ClassifierModule a class method by making it static:
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {
// this is now a method at class level
public static int measureRatio(double[] means) {
return (int) (means[3] / means[1]) ;
}
}
public static void main(String[] args) {
// define a parameter to be passed
double[] values = {3.0, 4.0}; // this is just an example array!
int r = ClassifierModule.measureRatio(values);
}
It depends on your requirements what option you should choose.

Passing an Array to Different Class...Netbeans Java [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
A.java
public Class A
{
String a,b;
public static void setArray(String[] array)//This is where i want the array to come
{
array[0]=a;
array[1]=b
}
}
B.java
public class B
{
String[] arr1 = new String[2];
arr1[0]="hello";
arr1[2]="world";
public static void main(String[] args)
{
A a = new A();
a.setArray(arr1);//This is from where i send the array
}
}
I am trying to send an array from one class to another class
I've edited your code a bit. Your main problem was in class A, where you were assigning values backwards. See the updated class A. I also added a constructor to your class, but this isn't strictly necessary.
public Class A {
String a,b;
// A public method with no return value
// and the same name as the class is a "class constructor"
// This is called when creating new A()
public A(String[] array)
{
setArray(array) // We will simply call setArray from here.
}
private void setArray(String[] array)
{
// Make sure you assign a to array[0],
// and not assign array[0] to a (which will clear this array)
a = array[0];
b = array[1];
}
}
public class B {
String[] arr1 = new String[2];
arr1[0]="hello";
arr1[2]="world";
// A a; // You can even store your A here for later use.
public static void main(String[] args)
{
A a = new A(arr1); // Pass arr1 to constructor when creating new A()
}
}
You were getting a NULL value because your String variables in class A were not initialized.
In class A you need to remove the STATIC from the method, and initialize the String a and b with something, like this:
public class A {
String a = "bye";
String b = "bye";
public void setArray(String[] array) {
array[0] = a;
array[1] = b;
}
}
In class B you should add STATIC to your array (you cannot reference a non-static variable within a static method).
public class B {
static String[] arr1 = {"hello", "world"};
public static void main(String[] args) {
A a = new A();
a.setArray(arr1);//This is from where i send the array
System.out.println(arr1[0] + " " + arr1[1]);
}
}
Also, if you want to initialize something the way you did (outside a method):
String[] arr1 = new String[2];
arr1[0]="hello";
arr1[2]="world";
you have to put the initialization within a block, like this:
String[] arr1 = new String[2];
{
arr1[0] = "hello";
arr1[2] = "world";
}
Hope this helps you

Accessing the max element from a list in another class - JAVA

for educational purposes I am trying to understand how to access a list's maximum element(originally in class B) (in this case from a Double list) through another class e.g class A. The list is used in a different class in which elements are added to it (e.g class C). However, when I add something like this to my class A to access my Max element, it does not seem to work: // help is appreciated :) and the error I usually get is noSuchElementException
just a method of class A
void printMax () {
B b = new B();
Double result;
result = Collections.max(b.array);
System.out.println("MAX:" +result);
}
here is my class B:
public class B {
public ArrayList<Double> array;
B() {
array = new ArrayList<Double>();
}
public void doSomething() {
int i;
for(i = 0; i < array.size(); i++) {
System.out.println("Doubles:" +array.get(i));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new B().doSomething();
}
}
Here is my class C that adds to my ArrayList.
Class C {
public String line;
C () {
}
public void linePicker() {
B b = new B();
Scanner dScanner = new Scanner(line);
while (dScanner.hasNext()) {
if (dScanner.hasNextDouble()) {
b.array.add(dScanner.nextDouble());
break;
} else {
dScanner.next();
}
}
dScanner.close();
b.doSomething();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new C().linePicker();
}
}
According to the javadocs (http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection,%20java.util.Comparator)), it throws that exception when the Collection is empty. You initialize b.array, but haven't added to it yet before calling max().

Print specific array object with method

I got a array of objects.
From another method i want to print one object from the array, the input to this method must be an integer, that represent the index of the object in the array.
I can't reach the array from printObject(). How do i do this?
public static void main(String[] args) {
Object []obj = new Object[2];
printObject(1);
}
public static void printObject(int i){
if (i == 0){
System.out.println(obj[0].toString());
}
if (i == 1){
Systen.out.println(obj[1].toString());
}
}
You could pass the array to printObject as a parameter (and simplify):
public static void main(String[] args) {
Object[] obj = new Object[2];
printObject(obj, 1);
}
public static void printObject(Object[] objects, int index){
if (index == 0 || index == 1) {
System.out.println(objects[index].toString());
}
}
Because it's declared inside the block of the main method, it will be known only there. Make it a class member or pass it as a parameter.
Example:
private int memberInt;
private void foo() {
memberInt = 5; // :)
int a = 7;
//..
a = 9; // :)
}
private void bar() {
a = 10; // :_(
memberInt = 10; // :)
}
The scope of the variable obj is limited to main method and will not be available in printObject method.
So to get access to variable of type Object[], make Object []obj as class member so that this member will be available through out the class or can be sent as an argument to printObject method.
Check the following code:
public class AccessingMembers
{
static Object []obj = null;
public static void main(String[] args) {
obj = new Object[2];
obj[1] = new Integer(10);//for example
printObject(1);
}
public static void printObject(int i){
if (i == 0){
System.out.println(obj[0].toString());
}
if (i == 1){
System.out.println(obj[1].toString());
}
}
}
If you run the code you'll get 10 as an answer.
either declare a global array which is accessible throughout the class or pass the array as a paramter to the method, so that it can access it.
Object []obj = new Object[2]; is a method variable and it's scope is only to that method.
Here there is one more thing using the above statement you created only two references of the object but not the instances.
//create instances
obj[0]=new Object();
obj[1]=new Object();
try this,
class Test {
static Object[] obj = new Object[2];
public static void main(String[] args) {
printObject(1);
}
public static void printObject(int i) {
obj[0]=new Object();
obj[1]=new Object();
if (i == 0) {
System.out.println(obj[0].toString());
}
if (i == 1) {
System.out.println(obj[1].toString());
}
}
}

Categories