4 Replies - 309 Views - Last Post: 30 October 2012 - 02:14 PM Rate Topic: -----

#1 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

StringTokenizer applet problem

Posted 30 October 2012 - 09:37 AM

The program reverse 10 integers from textfield 1 to textfield 2. How to remove those obsolete 0 if I don't enter up to 10 integer.

1234 3 45
result

0 0 0 0 0 0 0 45 3 1234 //remove the front zero

package reverseApplet;

// An applet program to reverse a sequence of up to 10 integers
// entered into a text window, and write them into another text
// window.  Based on TokenizerString class.

import java.applet.*;
import java.awt.*;
import java.util.*; // For StringTokenizer class

public class reversedApplet extends Applet {

	TextField f, g;

	public void init() { // This is a standard creation of 2 text fields.
		f = new TextField("", 35);
		f.setEditable(true);
		add(f);

		g = new TextField("", 35);
		g.setEditable(false);
		add(g);

		add(new Button("Reverse me!"));
	}

	// A method to handle button clicks and reverse the sequence of numbers.
	// Needs to be fixed to reverse only the actual number of integers entered.
	public boolean action(Event e, Object arg) {

		if (((Button) e.target).getLabel() == "Reverse me!") {
			String input = f.getText();
			String temp;
			int fromText[] = new int[10];

			StringTokenizer st = new StringTokenizer(input);
			for (int i = 0; i < fromText.length; i++)
				if (st.hasMoreTokens()) {
					temp = st.nextToken();
					fromText[i] = Integer.parseInt(temp);}

			g.setText("");
			for (int j = 0; j < fromText.length; j++)
				g.setText(g.getText() + " " + fromText[fromText.length - j - 1]);

		}

		return true;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: StringTokenizer applet problem

#2 kellzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: StringTokenizer applet problem

Posted 30 October 2012 - 09:56 AM

couldn't you just set the array to create only the number of elements needed. just count how many integers the user inputs then set the number of indices to that amount? idk if this is the answer you were looking for..

int fromText[] = new int[integerCount];


something like that

this link has some ideas on removing zeroes from arrays if you want to do it this way
http://stackoverflow...eros-from-array
Was This Post Helpful? 0
  • +
  • -

#3 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: StringTokenizer applet problem

Posted 30 October 2012 - 10:32 AM

View Postkellzor, on 30 October 2012 - 09:56 AM, said:

couldn't you just set the array to create only the number of elements needed. just count how many integers the user inputs then set the number of indices to that amount? idk if this is the answer you were looking for..

int fromText[] = new int[integerCount];


something like that

this link has some ideas on removing zeroes from arrays if you want to do it this way
http://stackoverflow...eros-from-array


I've tested it, but those method doesn't work with the tokenizer.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: StringTokenizer applet problem

Posted 30 October 2012 - 12:33 PM

StringTokenizer is legacy code use the String split() method inseat

Actually using getText().split(" ") will solve your problem as the returned array length will tell you how many token you have
Was This Post Helpful? 0
  • +
  • -

#5 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: StringTokenizer applet problem

Posted 30 October 2012 - 02:14 PM

Where do you add that line? When I tried to add in, it gave me some error

View Postpbl, on 30 October 2012 - 12:33 PM, said:

StringTokenizer is legacy code use the String split() method inseat

Actually using getText().split(" ") will solve your problem as the returned array length will tell you how many token you have

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1