LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2004, 08:38 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
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)
{

}
 
Old 02-18-2004, 08:42 PM   #2
retep
Member
 
Registered: Sep 2003
Distribution: RedHat/Debian
Posts: 50

Rep: Reputation: 15
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.
 
Old 02-18-2004, 08:49 PM   #3
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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?
 
Old 02-18-2004, 08:54 PM   #4
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
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;
}
}
 
Old 02-18-2004, 09:14 PM   #5
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
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.

Last edited by moeminhtun; 02-18-2004 at 09:15 PM.
 
Old 02-18-2004, 09:15 PM   #6
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
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)
 
Old 02-18-2004, 09:17 PM   #7
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
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)
 
Old 02-18-2004, 09:24 PM   #8
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
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()
}
 
Old 02-18-2004, 09:25 PM   #9
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
Thank you for all the help you gave me
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't type in Java applet using Firefox? JamieBrown Linux - Software 12 07-26-2010 01:52 AM
should i have any type of server set up to use java mail?? gajaykrishnan Programming 1 01-28-2005 03:39 AM
ap test - java - study help Laptop2250 Programming 7 05-04-2004 08:28 AM
java student with serialize type question Brain Drop Programming 11 03-29-2004 08:36 PM
java.lang.NullPointerException in JDBC test chr15t0 Programming 2 07-07-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration