
March 25th, 2004, 05:40 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
character at
I'm trying to write a program that will return the position that a character is in the alphabet, from a string entered by the user.
For example the user enters abc it would then print 123 below this. Or if they simply wrote z it would return the number 26.
My code however has an error, and i'm not quite sure whats going on, any help would be appreciated.
*the error occurs on the line "arrayinputted = new char[] alphapos.tochararray();".
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//A program to display the position in the alphabet of a character entered
publicclass lettercount extends Applet implements ActionListener
{
String alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
char[] arrayinputted;
String alphapos;
Label prompt;
TextField userinput;
publicvoid init()
{
setBackground(Color.blue);
prompt = new Label ("Please enter a character and the computer will tell you what position that character is in the alphabet");
userinput = new TextField(10);
add(prompt);
add(userinput);
userinput.addActionListener(this);
}
//Prints out the position of the character
publicvoid paint(Graphics g)
{
g.drawString("The letter(s) you entered are at " +alphapos+ " position in the alphabet", 20, 200);
}
//Finds out what position the character is at
publicvoid actionPerformed (ActionEvent e)
{
alphapos = userinput.getText();
arrayinputted = new char[alphapos.length()];
arrayinputted = newchar[] alphapos.tochararray();
repaint();
}
}
|