This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes re: trying to display a list of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "re: trying to display a list of arrays" Watch "re: trying to display a list of arrays" New topic
Author

re: trying to display a list of arrays

Savanna Ray
Greenhorn

Joined: Nov 20, 2004
Posts: 9
Hi,

I'm pretty new at Java, 2.5 months so far, and I'm trying to create a program that basically takes a query string from a form and breaks it up into name/value pairs and then prints the corresponding name with the value.

Here's the code so far:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

public class Labform2 extends Applet{

private Util util;
public void init() {
util = new Util(getGraphics());
}

public void paint(Graphics g) {

String query = "Surname=Smith&GivenName=Jane&SIN=123456789&age=21";
Form form = new Form(query);

util.display("The query string: " + query);
util.display("Name: " + form.getNames());
}
}

class Pair {

private String name;
private String value;

public Pair(String name, String value) {

this.name = name;
this.value = value;
}

public String getName() {

return name;
}

public String getValue() {

return value;
}

public void setName(String name) {
this.name = name;
}

public void setValue(String value) {
this.value = value;
}
}


class Form {

private Pair[] form;

public Form(String query) {

form = parsePairs(query);
}

public String[] getNames() {

String[] names = new String[form.length];
for(int i=0; i < names.length; i++) {
names[i] = form[i].getName();
}

return names;

}

public Pair[] parsePairs(String query) {

StringTokenizer tokens = new StringTokenizer(query, "&");
Pair[] pairs = new Pair[tokens.countTokens()];
int i = 0;
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
Pair p = parsePair(token);
pairs[i] = p;
System.out.println("names " + pairs[i]);
i++;
}
return pairs;
util.print(pairs);

}

private Pair parsePair(String nameValueString) {

int pos = nameValueString.indexOf('=');
String name = nameValueString.substring(0, pos);
String value = nameValueString.substring(pos + 1);
Pair p = new Pair(name, value);
return p;
}

public int charCount(String str, char search) {

int i=0;
int count = 0;

while (i < str.length()) {

char ch = str.charAt(i);
if (ch == search) {
count++;
}
i++;
}
return count;
}
}

class Util {

private Graphics g;
private int lineNr = 0;

public Util(Graphics g) {

this.g = g;
}

public void reset() {
this.lineNr = 0;
}

public void display(String msg) {
int x = 10;
int y = 15;
int incr = 15;
g.drawString(msg, x, y + incr * lineNr++);
}

public void display(String[] array) {

int length = array.length;
for (int i = 0; i < length; i++) {
display(" " + i + ": " + array[i]);
}
}
public void print(String[] array) {

int x = 10;
int y = 15;
int incr = 15;
for (int i=0; i < array.length; i++) {

g.drawString("" + array[i], x, y * i);
}
}
}

It does compile, but it doesn't display the array variables of form properly for some reason. Line 19: util.display("Name: " + form.getNames());
should be displaying:

Surname
GivenName
Age
SIN

But for some reason isn't doing that. Is it because the method getNames has an error in it?
If there's anyone who could give some feedback in how to fix this code, it would really be much appreciated.

Thanks!
Happy Java Coding
B.C.Canada
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
In your paint method you are calling the getNames() method of the form Object:But form.getNames() doesn't return a String that you can display, it returns an ARRAY of Strings. When you try to print it, it prints a nonsenical String representation of the array object - like java.lang.String;@7ced01.

Hope that helps.


IBM 286, SCJP, SCWCD, EIEIO
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Another opportunity ... when you're parsing all the pairs you have a print of pairs[i]. What kind of output does that give you? The JVM calls the toString() method of your Pair class. Since you didn't code toString() it calls the default method on the class Object which doesn't give very nice results. If you code a toString() method you can make it display something like:

Name=xxxx Value=yyyy

which will be nifty for debugging or even reporting your pairs. Hope that's helpful!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Savanna Ray
Greenhorn

Joined: Nov 20, 2004
Posts: 9
Hi Rick,

yes, I thought that the statement: util.display("Name: " + form.getNames());

would display all the names, so am I going about this the wrong way?

How about: creating a loop to display each individual element in the array?

i=0
while (i < names.length) {
util.display("Name: " + form.getNames(names[i]));
i++;
}

would this work?

Thanks from BC
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
Sure. But I would use a for loop:
You're welcome from Boston. ;-)
Savanna Ray
Greenhorn

Joined: Nov 20, 2004
Posts: 9
Actually, the method:
util.display("Name: " + form.getNames(names[i]));

didn't work, instead I got an error message saying it couldn't find variable names for some reason which is in my Form class.

How would I reference the array and print all the elements then?

BC
Savanna Ray
Greenhorn

Joined: Nov 20, 2004
Posts: 9
How would I reference the array and print all the elements from the method "paint"? Is it possible?

Thanks again..!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: re: trying to display a list of arrays