Results 1 to 5 of 5
  1. #1
    eyeore is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Smile need help in string to input

    hello everyone im a little confuse on how to do this string to input could you kindly help me here my code
    i dont know how to make code to make the user input instead of me i like the code to be flexible instead of fixed please help me

    Java 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);
      }
    }
    Java Code:
    import java.util.Scanner;
    
    
    public class TestString {
    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);
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default

    Read Sun's tutorials on I/O; pay special attention to the BufferedReader class and the Scanner class.

    kind regards,

    Jos

  3. #3
    abollm is offline Member
    Join Date
    Jan 2010
    Location
    D-Munich
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by eyeore View Post
    hello everyone im a little confuse on how to do this string to input could you kindly help me here my code
    i dont know how to make code to make the user input instead of me i like the code to be flexible instead of fixed please help me
    [..]
    Just to give you some ideas:

    1. Write a class for input values (String, int etc.)
    2. Write a method for processing the input value
    3. Start your program (Good luck :-)

  4. #4
    eyeore is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    hello thanks alot now im only problem is how to display after the reverse could teach me where to put it here my new code i dont know where to put after the reverse could tell me where to put it please

    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);
    }
    }
    }

  5. #5
    abollm is offline Member
    Join Date
    Jan 2010
    Location
    D-Munich
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by eyeore View Post
    hello thanks alot now im only problem is how to display after the reverse could teach me where to put it here my new code i dont know where to put after the reverse could tell me where to put it please
    [..]
    }
    First:
    For the future: Don't forget to use the "Code Formatting Tags" so the posted code retains its original formatting. You can do it by selecting the code and then clicking on the "Code" button "#" above.

    Just insert three lines of code, see below:

    Java 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);
    
    		String reverse = "";
    		for (int j = 0; j < name.length(); j++) {
    			char ch = name.charAt(j);
    			theStack.push(ch);
    			reverse = ch+reverse;
    		}
    
    		while (!theStack.isEmpty()) {
    			char ch = theStack.pop();
    			System.out.println(ch);
    		}
    		System.out.println(reverse);
    	}
    }
    Last edited by abollm; 01-11-2010 at 12:09 PM.

Similar Threads

  1. Jcombobox not liking String input for options :|
    By skatefreak in forum New To Java
    Replies: 4
    Last Post: 05-11-2009, 01:34 PM
  2. [SOLVED] Reading an input string?!
    By sfe23 in forum New To Java
    Replies: 6
    Last Post: 02-23-2009, 05:38 AM
  3. How to Mask input string
    By nmc.091 in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 04:49 AM
  4. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 07:23 PM

Posting Permissions

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