Results 1 to 6 of 6
  1. #1
    umer25 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default need help outputting a string in reverse within an applet. plzzzz help!!!

    Hi im a first year uni student with no prior experience in programming and im really struggling with applets atm :( the user is meant to enter a word in my program and it is supposed to be displayed in reverse on the screen. i cant seem to figure out how to do it. i am trying to create a separate method which contains the reverse string code but no matter what i try, it doesnt seem to work -.- someone pleeeease help heres my code and the errors which i am getting:

    //import java.util.*;
    import java.applet.Applet;
    import java.awt.*; // import the java.awt package
    import java.awt.event.*; // import the java.awt.event package

    public class Text1 extends Applet implements ActionListener
    {
    String pr_name;
    TextField pr_input;

    public void init()
    //pre:
    //post: adds TextField to applet
    {
    pr_input = new TextField(20);
    add(pr_input);
    pr_input.addActionListener(this);
    }

    public void start()
    //pre:
    //post: initializes name blank
    {
    pr_name = "";
    }

    public void actionPerformed(ActionEvent e)
    //pre: event driven by any component action
    //post: sets name to action source, repaints
    {
    pr_name = e.getActionCommand();

    reverse(text);

    repaint();
    }

    public void paint(Graphics g)
    //pre: g is valid graphics object
    //post: draws to Applet window
    {
    g.drawString(pr_name,25,100);
    }


    public String reverse(String text)
    {
    if (pr_name.length() > 1)
    {
    String reversed = "";

    for (int i = pr_name.length() - 1; i >= 0; i--)
    {
    reversed += Character.toString(text.charAt(i));
    }

    return reversed;

    }
    else { return text; }

    }
    }

    ERROR:

    Error: text cannot be resolved to a variable

    I REALISE THAT I NEED TO PLACE A STRING VALUE WHEN I AM CALLING UPON THE METHOD BUT I WANT THE USERS INPUT TO BE REVERSED AND I DONT KNOW HOW TO DO IT. really appreciate some help. thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: need help outputting a string in reverse within an applet. plzzzz help!!!

    Where do you want the output to go?
    Presumably the input is in the text field?

    And can you please wrap your code in [code] tags [/code] so it retains its formatting and is readable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    umer25 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: need help outputting a string in reverse within an applet. plzzzz help!!!

    oh sorrrry thats what code tags are for :/ lol.

    Java Code:
    //import java.util.*;
    import java.applet.Applet;
    import java.awt.*; // import the java.awt package
    import java.awt.event.*;    // import the java.awt.event package
    
    public class Text1 extends Applet implements ActionListener
    {
        String pr_name;
        TextField pr_input;
    
        public void init()
        //pre:
        //post: adds TextField to applet
        {
            pr_input = new TextField(20);
            add(pr_input);
            pr_input.addActionListener(this);
        }
    
        public void start()
        //pre:
        //post: initializes name blank
        {
            pr_name = "";
        }
    
        public void actionPerformed(ActionEvent e)
        //pre: event driven by any component action
        //post: sets name to action source, repaints
        {
            pr_name = e.getActionCommand();
            
             reverse(text);
            
            repaint();
        }
    
        public void paint(Graphics g)
        //pre: g is valid graphics object
        //post: draws to Applet window
        {
            g.drawString(pr_name,25,100);
        }
        
        
        public String reverse(String text) 
        {
          if (pr_name.length() > 1) 
        {
            String reversed = "";
    
         for (int i = pr_name.length() - 1; i >= 0; i--) 
         {
         reversed += Character.toString(text.charAt(i));
         }
    
         return reversed;
    
       }
    else { return text; }
    
    }
    }
    initially i didnt think i needed a seperate method but figured it would be easier. ive tried putting a decrementing for loop within the actionPerformed method but still no luck.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: need help outputting a string in reverse within an applet. plzzzz help!!!

    You haven't answered my questions.
    How are you planning on outputting the result?

    Your reverse() method looks fine, not that I've run it.
    You just need to do something with the result inside the actionPerformed().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    umer25 is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: need help outputting a string in reverse within an applet. plzzzz help!!!

    there will be a simple string below the input box which displays the inputted string in reverse. if thats the answer youre not looking for then im not quite sure what you mean what am i meant to put within the actionPerformed method to make it work? im stumped

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: need help outputting a string in reverse within an applet. plzzzz help!!!

    So you need a JLabel (or similar) to put the result into.
    And then give it a new value in the actionPerformed, from the value returned by reverse().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help with outputting a specific string array
    By wtbarr in forum New To Java
    Replies: 7
    Last Post: 02-11-2013, 01:46 PM
  2. Replies: 10
    Last Post: 03-15-2012, 10:46 AM
  3. reverse string
    By mallikanala in forum New To Java
    Replies: 1
    Last Post: 01-21-2012, 03:40 PM
  4. How to reverse a string?
    By Neeer in forum New To Java
    Replies: 17
    Last Post: 03-27-2011, 08:10 PM
  5. Reverse A String Without Allocating A New String
    By marco.c84 in forum Advanced Java
    Replies: 10
    Last Post: 03-22-2011, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •