ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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]++;
}
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
}
}
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);
}
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.
Last edited by moeminhtun; 02-18-2004 at 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)
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)
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()
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.