LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   test type in java (https://www.linuxquestions.org/questions/programming-9/test-type-in-java-147811/)

exodist 02-18-2004 08:38 PM

test type in java
 
I am traying to move double values from an array with a length of 100 to a string array with 100 values, however only about 10 of the values in the double array are used most of the time, the rest are empty, (sometimes it does climb to 100 so I need to keep it there) I run into a problem of cource when it tries to move one of these empty values into the string array.

is there an if test liek this I can use?:

if (value[counter] is initilized)
{

}

retep 02-18-2004 08:42 PM

Did you mean a double[] or a Double[]? Java initializes all its primitive values. e.g. double d[] = new double[100]; would set all the values to 0. And Double d[] = new Double[100] would set them all to null.

So just use if(d[i]==0) or if(d[i]==null) to figure out if the value is 'set' or not.

moeminhtun 02-18-2004 08:49 PM

Re: test type in java
 
Quote:

Originally posted by exodist
I am traying to move double values from an array with a length of 100 to a string array with 100 values, however only about 10 of the values in the double array are used most of the time, the rest are empty, (sometimes it does climb to 100 so I need to keep it there) I run into a problem of cource when it tries to move one of these empty values into the string array.

is there an if test liek this I can use?:

if (value[counter] is initilized)
{

}

The primitive arrays are automatically initialized by 0 once you create it. What problem you run into?

exodist 02-18-2004 08:54 PM

I will post error and entire code, sorry if this makes anyone mad but I am kinda lost, please do not ask WHY I am doing this.

error:

exodist@Abydos:~/School/java/2/source$ javac ./stringMath.java
exodist@Abydos:~/School/java/2/source$ appletviewer ./test.html
java.lang.NullPointerException
at NumberType.<init>(stringMath.java:80)
at stringMath.<init>(stringMath.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:308)
at java.lang.Class.newInstance(Class.java:261)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:617)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:546)
at sun.applet.AppletPanel.run(AppletPanel.java:298)
at java.lang.Thread.run(Thread.java:534)

Code:

import java.awt.*;
import java.applet.Applet;
import java.util.*;
import java.awt.event.*;
public class stringMath extends Applet implements ActionListener
{
private TextField Input;
private double[] Values = new double[100];
private char[] Operators = new char[100];
private int[] counter = new int[10];
private String inString = new String("0 + 0");
private NumberType TheNumber = new NumberType(Operators, Values);

public void init()
{
Input = new TextField(50);
add(Input);
Input.addActionListener(this);
}

public void actionPerformed(ActionEvent event)
{
inString = Input.getText();

MyParser(inString);

TheNumber = new NumberType(Operators, Values);

repaint();
}

public void paint (Graphics g)
{
g.drawString("The answer is: "+ TheNumber.GetAnswer(),60,60); // + TheNumber.GetAnswer()
}

public void MyParser(String FullString)
{
// I am gonna do more than the + and - :-D
StringTokenizer PToken = new StringTokenizer(FullString," +-/*()");
char theC;
counter[0] = 0;
//place values into an integer array to keep them in order, but seperate.
while (PToken.hasMoreTokens())
{
Values[counter[0]] = Double.parseDouble(PToken.nextToken());
counter[0]++;
}

counter[1] = 0;
counter[2] = 0;
while (counter[1] < FullString.length())
{
theC = FullString.charAt(counter[1]);

if ((theC == '+') || (theC == '-') || (theC == '/') || (theC == '*') || (theC == '(') || (theC == ')'))
{
Operators[counter[2]] = theC;
counter[2]++;
}
counter[1]++;
}
}
}

class NumberType
{
private double Answer = 0;
private char[] Opers;
private String[] Values;
private int[] counter = new int [10]; // I use a lot of counters and I am tired of declairing a new one each time.
private String[] Integrater = new String[100];

public NumberType(char[] Ops, double[] Vals)
{
Opers = Ops;
counter[0] = 0;
while (counter[0] < 100)
{
Values[counter[0]] = new String("" + (Vals[counter[0]]));
}

FirstStage();
SecondStage();
}

private void FirstStage() // Combines values and operators into a single array
{
counter[6] = 0;
while (counter[6] < 100)
{
Integrater[counter[6]] = "Z";
counter[6]++;
}

counter[0] = 0; // Keeps track of which Integrater index we are at.
counter[1] = 0; // keeps track of Operator index
counter[2] = 0; // keeps track of Value index.
//this will check and see if '(' is the first operator, if it is place the operator in first, if it is not place value in first, then place operator then go again, and if the next operator is '(' place it vefore next value because you may get this: (1 + (1 - 2))
while (counter[2] < Values.length)
{
if (Opers[counter[1]] == '(')
{
Integrater[counter[0]] = new String("" + Opers[counter[1]]);
counter[1]++;
counter[0]++;
}
Integrater[counter[0]] = new String("" + Values[counter[2]]);
counter[2]++;
counter[0]++;

Integrater[counter[0]] = new String("" + Opers[counter[1]]);
counter[1]++;
counter[0]++;
}
}

private void SecondStage() // Uses 2 arrays to shrink down the work, first by doing work inside '()' then doing * and / in order of left to right, then finally addition, then finally finishes.
{
String[] S1 = Integrater;
String[] S2 = new String[100];
S2 = FuncA(0, S1); // Do all Calculations inside '()'
S2 = FuncOO(S2); // Do Multiplications and divides from left to right
Answer = FuncPM(S2); // Addition and Subtraction from left to right
}

private String[] FuncA(int Start, String[] SS)
{
int End;
String[] S1 = SS;
int subCount = Start; // Can't use a global counter for a recursive function, lucky I caught it before I finished function

while (subCount < GetUsage(SS))
{
if (S1[subCount] == "(")
{
S1 = FuncA(subCount, S1); // recurse until the deepest ( is found.
}
else //if this one ends before next begins finish it out.
{
if (S1[subCount] == ")")
{
End = subCount;
S1 = ReString(S1, Start, End);
return S1; // every time it is called it should end w/ this return.
}
}
subCount++;
}
if (subCount == GetUsage(SS))
{
//Error! Output error to console cause something went wrong, probably missing a closing ')'
}
return S1; // it should never get here!
}

private String[] ReString(String[] SS, int Start, int End)
{
Start++; // We do not need the Brace anymore
End--; // " "
String[] S3 = new String[100];
String[] S4 = new String[100];
double RepVar; // Variable we will be replacing the bracketed section with.
int inCount = 0;
int inCount2 = 0;

//first lets copy string values except for this set of brackets.
while (inCount < GetUsage(SS))
{
S3[inCount] = SS[inCount2];
inCount++;
inCount2++;
if (inCount == (Start - 1))
{
inCount2 = (End + 2);
inCount++; // skip this index to place bracket value into next time
}
}

inCount = Start;
while (inCount < End)
{
S4[inCount] = SS[inCount];
inCount++;
}

S3[(Start + 1)] = new String("" + (FuncPM(FuncOO(S4))));

return S3;
}

private String[] FuncOO(String[] SS) //Multiply and divide from left to right
{
int MCounter = 1;
int MCounter2 = 0;
boolean lastValIn = false;
String[] S1 = new String[100];
while (MCounter < GetUsage(SS))
{
if ((SS[MCounter] == "*") || (SS[MCounter] == "/"))
{
S1[MCounter2] = new String("" + MD(SS, MCounter, (Double.parseDouble(SS[MCounter - 1]))));
MCounter2++;
while ((SS[MCounter] == "*") || (SS[MCounter] == "/"))
{
MCounter++;
MCounter++;
}
lastValIn = true;
}
else // Add next Operator to array
{
if (lastValIn)
{
S1[MCounter2] = SS[MCounter];
MCounter2++;
}
else
{
S1[MCounter2] = SS[MCounter - 1];
MCounter2++;

S1[MCounter2] = SS[MCounter];
MCounter2++;
}
lastValIn = false;
MCounter++;
MCounter++;
}
}
return S1;
}

private double MD(String[] SS, int Idx, double OU)
{
if ((SS[Idx] == "*") || (SS[Idx] == "/"))
{
if (SS[Idx] == "*")
{
OU = (OU * Double.parseDouble(SS[Idx + 1]));
}
if (SS[Idx] == "/")
{
OU = (OU / Double.parseDouble(SS[Idx + 1]));
}
if ((SS[Idx + 2] == "*") || (SS[Idx + 2] == "/"))
{
OU = MD(SS, (Idx + 2), OU);
}
}
return OU;
}

private double FuncPM(String[] SS) //Add and Subtract from left to right
{
double FNum = Double.parseDouble(SS[0]);
int counterpm = 1;
while (counterpm < GetUsage(SS))
{
if (SS[counterpm] == "+")
{
counterpm++;
FNum = FNum + Double.parseDouble(SS[counterpm]);
}
else
{
if (SS[counterpm] == "-")
{
counterpm++;
FNum = FNum - Double.parseDouble(SS[counterpm]);
}
}
counterpm++;
}
return FNum;
}

public int GetUsage(String[] One2Test) // whatever.length does nto work when some indexes are not used.
{
int RealUse = 0;
while (One2Test[RealUse + 1] != "Z")
{
RealUse++;
}
return (RealUse + 1);
}

public double GetAnswer()
{
return Answer;
}
}

moeminhtun 02-18-2004 09:14 PM

The problem is in this part of code.

<code>

class NumberType
{
private double Answer = 0;
private char[] Opers;
private String[] Values; // should be "String[] Values = new String[100];
private int[] counter = new int [10]; // I use a lot of counters and I am tired of declairing a new one each time.
private String[] Integrater = new String[100];

public NumberType(char[] Ops, double[] Vals)
{
Opers = Ops;
counter[0] = 0;
while (counter[0] < 100)
{
Values[counter[0]] = new String("" + (Vals[counter[0]])); // Error: the String array "Values" have not created.
}

</code>



Did you notice that your "Values" string array haven't created?
I put the comments in the above codes for the error and correction.

exodist 02-18-2004 09:15 PM

Sorry need to make a correction, line 12
private NumberType TheNumber = new NumberType(Operators, Values);

should be:
private NumberType TheNumber;

and the error is:
java.lang.NullPointerException
at stringMath.paint(stringMath.java:34)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.motif.MComponentPeer.handleEvent(MComponentPeer.java:405)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

exodist 02-18-2004 09:17 PM

I made my above correction, and also the correction moeminhtun pointed out and got this error:

java.lang.NullPointerException
at stringMath.paint(stringMath.java:34)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.motif.MComponentPeer.handleEvent(MComponentPeer.java:405)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

exodist 02-18-2004 09:24 PM

sorry to correct once again, I changed the code again, now it no longer gives error, but I think I have an infinit loop somewhere.
to correct error I changed this:

public class stringMath extends Applet implements ActionListener
{
private TextField Input;
private double[] Values = new double[100];
private char[] Operators = new char[100];
private int[] counter = new int[10];
private String inString = new String("0 + 0");
private NumberType TheNumber;

public void init()
{
Input = new TextField(50);
add(Input);
Input.addActionListener(this);
}

public void actionPerformed(ActionEvent event)
{
inString = Input.getText();

repaint();
}

public void paint (Graphics g)
{
MyParser(inString);

TheNumber = new NumberType(Operators, Values);

g.drawString("The answer is: " + TheNumber.GetAnswer(),60,60); // + TheNumber.GetAnswer()
}

exodist 02-18-2004 09:25 PM

Thank you for all the help you gave me


All times are GMT -5. The time now is 01:50 PM.