help on strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • appledon
    New Member
    • Jan 2010
    • 4

    help on strings

    hello everyone could teach me how to make the user input instead of me
    here my code i just needed my teststack to let the user input instead of me could tell me what to do ?

    Code:
    class Stack {
      private int maxSize;
    
      private char[] stackArray;
    
      private int top;
    
      public Stack(int s) {
        maxSize = s;
        stackArray = new char[maxSize];
        top = -1; 
      }
    
      public void push(char j) {
        stackArray[++top] = j;
      }
    
      public char pop() {
        return stackArray[top--];
      }
    
      public char peek() {
        return stackArray[top];
      }
    
      public boolean isEmpty() {
        return (top == -1);
      }
    
      public boolean isFull() {
        return (top == maxSize - 1);
      }
    }
    Code:
    import java.util.Scanner;
    
    
    public class TestStack {
    public static void main(String[] args) {
    String STS = " STS";
    int stackSize = STS.length();
    
    Stack theStack = new Stack(stackSize);
    
    
    for (int j = 0; j < STI.length(); j++) {
    char ch = STS.charAt(j);
    theStack.push(ch);
    }
    
    while (!theStack.isEmpty()) {
    char ch = theStack.pop();
    System.out.println(ch);
    }
    }
    }
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Use the Scanner (which you've imported...) to get the user's input.

    Have a read of the API documentation to find useful methods for obtaining a string from the user.

    Comment

    • appledon
      New Member
      • Jan 2010
      • 4

      #3
      i mean where do i put the inputs im really lose when it comes to let the user input im always fixed i dont let the user input but you point me to the right direction where to put this input string code in my stack/string code and which line should i start with when it comes to this input stuff im super lose and my code all about reversing word output like this

      before: STI
      after:ITS

      my code works but my professor wants me to let the user input he such a pain in the ass to me but i have to do it my problem is i really don't know where to start input and which line should it be could please point me to the right direction please

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        You can identify in your existing code where (the only time) the string is assigned to. And you have the API docs that describe how to get user input.

        I can't see what else I can do short of writing teh codes. And I'm not going to do that (principally because I don't see that as helping.) Consult your textbook and make an attempt! In all sincerity, if you can't take the next step forward you really should talk to your teacher.

        Comment

        • appledon
          New Member
          • Jan 2010
          • 4

          #5
          thanks alot man now my only problem is how to display after the reversal could give me a little tip on how to do it please here my code please where do i put the after the reversal code please i really need help here just a little im still new it i already broke my brain think this please where do i put the after reverse code

          Code:
          import java.util.Scanner;
          
          public class TestStack {
            public static void main(String[] args) {
          	Scanner scan = new Scanner(System.in);
              String name =null;
          	System.out.println("Input your name: ");
          	name = scan.nextLine();
          	System.out.println(" before: " + name);
              int stackSize = name.length();
          
              Stack theStack = new Stack(stackSize);
          
              for (int j = 0; j < name.length(); j++) {
                char ch = name.charAt(j);
                theStack.push(ch);
              }
          
              while (!theStack.isEmpty()) {
                char ch = theStack.pop();
                System.out.println(ch);
              }
            }
          }
          Last edited by Frinavale; Jan 13 '10, 03:49 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

          Comment

          Working...