The Artima Developer Community
Sponsored Link

Java Answers Forum
a little help needed.

2 replies on 1 page. Most recent reply: Nov 22, 2002 11:57 AM by uh

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
uh

Posts: 5
Nickname: chickenman
Registered: Nov, 2002

a little help needed. Posted: Nov 20, 2002 3:30 PM
Reply to this message Reply
Advertisement
/*program should allow user to enter a name for example John Doe Hue. it allows the user to print the name out in 3 different formats formal, reversed, pig latin.
formal- John D. Hue
reversed- nhoj eod euh
pig latin- ohnjay eoday euhay //ohn + j+ ay

problem is im getting an error when i enter the name it just prints null. can anyone just copy and paste the code below into some editor, compile personname,it's sub classes & test. then just run test.
*/
//*******************************************************888

public class PersonName extends Object
{ // protected

public PersonName(String first, String middle, String last)
{
setName(first,middle,last);
}


public void setName(String first, String middle, String last)
{ String f,m,l;
f=first;
m=middle;


public String toString()
{
return f + ""+ m +""+ l;
}
}
//*******************************************************888

public class FormalName extends PersonName
{ protected String s;
protected char newM;

public FormalName(String first, String middle, String last)
{
super(first,middle,last);

}



public String toString()
{ //String newM, output;
//char charArray[];
//int x = s.length();

//s.getChars(0,x, charArray, 0);

s=m;
char[] c = s.toCharArray();
int l = c.length;
for( int i=0; i<l;i++ ){

newM = c[ i ];
}




//char charArray[] = s;
//for (int count=0; count < charArray.length; count++ )
//output+= charArray[count] ;
//StringBuffer buffer= new StringBuffer ( m );
// newM= buffer.chatAt(0);
//newM= output.charAt[0];

return f + " "+ newM + "."+ l;


}
}
//*******************************************************888
public class ReverseName extends FormalName
{ protected String ff,mm,ll,ts,ts1,ts2;

public ReverseName(String first, String middle, String last)
{
super(first,middle,last);
setRight(first,middle,last);

}



public void setRight(String first, String middle, String last)
{
ff=first.toLowerCase();
mm=middle.toLowerCase();
ll=last.toLowerCase();

setFirst(ff);
setMiddle(mm);
setLast(ll);
}

public void setFirst(String t)
{ String temp;
temp = t;
for (int count = temp.length() - 1; count >=0; count-- )
ts+= temp.charAt(count)+ "";
}

public void setMiddle(String t1)
{ String temp1;
temp1 = t1;

for (int count = temp1.length() - 1; count >=0; count-- )
ts1+= temp1.charAt(count)+ "";
}

public void setLast(String t2)
{ String temp2;
temp2 = t2;
for (int count = temp2.length() - 1; count >=0; count-- )
ts2+= temp2.charAt(count)+ "";
}


public String toString()
{

return ts + " "+ ts1 +" "+ ts2;


}
}
//******************************************************888
public class PigLatinName extends ReverseName
{ protected String z;

public PigLatinName(String first, String middle, String last)
{
super(first,middle,last);
setRight(first,middle,last);

}



public void SetRight(String first, String middle, String last)
{
int x= first.length();
int y= middle.length();
int r= last.length();

String a,b,c;
char p,l,j;
String lfirst,lmiddle,llast;

a = first.substring(1,x);
b = middle.substring(1,y);
c = last.substring(1,r);

p=first.charAt(0);
l=middle.charAt(0);
j=last.charAt(0);

lfirst= a+p+z;
lmiddle=b+l+z;
llast= c+j+z;


String z= new String ("ay");

}


public String toString()
{
return f + " "+ m +" "+ l;


}
}
//*******************************************************888
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class test extends JFrame
{
PersonName name;
private JLabel prompt;
private JTextField input;
private JTextArea outputArea;
private JButton formal, reversed, latin;// exit;

public test()
{
super("Person Name Tester");


Container c= getContentPane();
c.setLayout(new FlowLayout() );


prompt= new JLabel("Enter name & press Enter:");
c.add(prompt);
input = new JTextField(20);

input.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent event)
{
String s =event.getActionCommand();
outputArea.setText ( " " +name);
}
}
);
c.add(input);



formal= new JButton("Formal Name");
formal.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent event)
{
String s =event.getActionCommand();
outputArea.setText ( " " +name);
}
}
);
c.add(formal);

reversed= new JButton("Reverse Name");
reversed.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent event)
{
String s =event.getActionCommand();
outputArea.setText ( " " +name);
}
}
);
c.add(reversed);

latin= new JButton("Latin Name");
latin.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent event)
{
String s =event.getActionCommand();
outputArea.setText ( " " +name);
}
}
);
c.add(latin);

outputArea = new JTextArea(15,20);//width,height panel
outputArea.setEditable(false);
c.add(new JScrollPane(outputArea));
setSize(289,410);// width,height Win grey
show();
}

public static void main(String args[])
{
test window = new test();

window.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
}
);
}
}


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: a little help needed. Posted: Nov 21, 2002 3:19 AM
Reply to this message Reply
Ok, i have written some code for your problem. Can't gurantee thats right but you may check. Now if a person does not enter a middlename, you will get an exception. so if thats a criteria, you may want to include it. here's the basic code (i've used your gui code): hope that solves your problem :)

public class personName extends JFrame
{


private JLabel prompt;
private JTextField input;
private JTextArea outputArea;
private JButton formal, reversed, latin;
// exit;

public personName(){
Container c= getContentPane();
c.setLayout(new FlowLayout());
prompt= new JLabel("Enter name & press Enter:");
c.add(prompt);
input = new JTextField(20);
input.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event){
String s =event.getActionCommand();
outputArea.setText(s);
}
});
c.add(input);

formal= new JButton("Formal Name");
formal.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event){
//String s =event.getActionCommand();
String str = input.getText();
String strFinal = process(str,"formal");
outputArea.setText(strFinal);
}
});
c.add(formal);

reversed= new JButton("Reverse Name");
reversed.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event){
//String s =event.getActionCommand();
String str = input.getText();
String strFinal = process(str,"reverse");
outputArea.setText(strFinal);
}
});
c.add(reversed);

latin= new JButton("Latin Name");
latin.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent event){
//String s =event.getActionCommand();
String str = input.getText();
String strFinal = process(str,"pigLatin");
outputArea.setText(strFinal);
}
});
c.add(latin);

outputArea = new JTextArea(15,20);//width,height panel
outputArea.setEditable(false);
c.add(new JScrollPane(outputArea));
setSize(289,410);// width,height Win grey
show();
}

// This method returns the Formal String
private String formal(String first, String middle, String last){

for (int i=0;i<middle.length() ;i++ )
{
middle = middle.substring(0,1)+".";
}

first = first.substring(0,1).toUpperCase() + first.substring(1,first.length());
middle = middle.substring(0,1).toUpperCase() + middle.substring(1,middle.length());
last = last.substring(0,1).toUpperCase() + last.substring(1,last.length());

return first+" "+middle+" "+last;
}

// This returns the reverse of the String
private String reverse(String str)
{
str = str.toLowerCase();
String reverseIt = "";
for (int j=str.length()-1;j >= 0 ;j-- )
{
reverseIt = reverseIt + str.charAt(j);
}
return reverseIt;
}

// And this one the pigLatin version but i'm sure not sure of how you've processed pigLatin
private String pigLatin(String piggy){

piggy = piggy.toLowerCase();
piggy = piggy.substring(1,piggy.length()) + piggy.substring(0,1) + "ay";

return piggy;
}

// This one makes the Strings separate and then processes it accordingly
private String process(String str, String process){
String first = str.substring(0,str.indexOf(" "));
String middle = str.substring(str.indexOf(" ")+1,str.lastIndexOf(" "));
String last = str.substring(str.lastIndexOf(" ")+1,str.length());
String processed = "";

if (process.equals("formal"))
{
processed = formal(first,middle,last);
}else if (process.equals("reverse"))
{
processed = reverse(first)+" "+reverse(middle)+" "+reverse(last);
}else{
processed = pigLatin(first)+" "+pigLatin(middle)+" "+pigLatin(last);
}
return processed;
}

public static void main(String[] args){
personName pn = new personName();
pn.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}

}

uh

Posts: 5
Nickname: chickenman
Registered: Nov, 2002

Re: a little help needed. Posted: Nov 22, 2002 11:57 AM
Reply to this message Reply
hey sorry i did not reply earlier but i want to thank you the code it fixed everything. middle name exception was not part of what the program should do but i'll throw it in there anyway since it makes sense. thanks again.

Flat View: This topic has 2 replies on 1 page
Topic: Arithmetic java operators Previous Topic   Next Topic Topic: to read a file rtf whit java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use