Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

re: trying to display a list of arrays

 
Savanna Ray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Savanna Ray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. But I would use a for loop:
You're welcome from Boston. ;-)
 
Savanna Ray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I reference the array and print all the elements from the method "paint"? Is it possible?

Thanks again..!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic