In line 32 which is the lttr[ctr] = read.next().charAt(0); wont read all the letters that i input. it only reads the last letter that i entered. what should i do to be able to display all the letters i entered and be able to reverse there order so that they would act as a PUSH. Line 34 is required every after a letter was input by the user. and if you have another solution so that the user can use a 'Y' and 'N' as a reply instead of 1 or 0. Thanks

lttr --- is my array that reads the letter that the user inputs. it can only hold 10 arrays.
ctr --- is just for the loop.

package javaapplication4;

import java.util.*;

public class PushPop {

    static Scanner read = new Scanner (System.in);

    public static void main(String[] args) {
       
        char letter;
        char[] lttr = new char[10];
        int ctr=0,again;

        System.out.println ("Main Menu");
        System.out.println ("A.Push");
        System.out.println ("B.Pop");
        System.out.println ("C.Print");
        System.out.println ("D.exit");
        System.out.print   ("Please enter a letter:");
        letter = read.next().charAt(0);

        switch (letter)
        {
            case 'a':
            case 'A':
                     System.out.println ("Push");
                     do
                     {
                     
                     System.out.println ("Enter a letter:");
                     lttr[ctr] = read.next().charAt(0);
                     
                     System.out.println ("Again? 1 for yes 0 for no");
                     again = read.nextInt();
                     }while (again>0);

                     for (ctr=lttr.length-1;ctr>=0;ctr--)
                     {
                     System.out.print (lttr[ctr]+" ");
                     }
                     break;
        }


    }


}

Where do you increment ctr after pushing?

Thats the problem.. i don't know where to increment the ctr

It starts at 0.
You push the next char into element [0]
Where should the next char go?
It doesn't matter exactly where you increment it, as long as its after pushing one char and before pushing the next.

package javaapplication4;

import java.util.*;

public class PushPop {

    static Scanner read = new Scanner (System.in);

    public static void main(String[] args) {

        char letter;
        char[] lttr = new char[10];
        int ctr=0,again=0,exit;

        System.out.println ("Main Menu");
        System.out.println ("A.Push");
        System.out.println ("B.Pop");
        System.out.println ("C.Print");
        System.out.println ("D.exit");
        System.out.print   ("Please enter a letter:");
        letter = read.next().charAt(0);

        switch (letter)
        {
            case 'a':
            case 'A':
                     System.out.println ("Push");


                     do
                     {
                     System.out.println ("Enter a letter:");
                     lttr[ctr] = read.next().charAt(0);
                      ctr++;

                     System.out.println ("Again? 1 for yes 0 for no");
                     again = read.nextInt();
                     }while (again>0);

                     for (ctr=ctr-1;ctr>=0;ctr--)
                     {
                     System.out.print (lttr[ctr]+" ");
                     }
                     break;
            case 'b':
            case 'B':
                     System.out.println ("Pop");
                     do
                     {

                     System.out.println (lttr[ctr]+ " ");

                     System.out.println("Again? 1 for yes 0 for no");
                     again = read.nextInt();
                     }while (again>0);
                     break;
            case 'c':
            case 'C':
                    System.out.println ("Print");

                    System.out.println(lttr[ctr]+ " ");

                    break;

        }

    }


}

ok... solved my own problem... hahaha anyway.. how do i show up the menu again after case A has finished? and will the elements of the array stay?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

1. You need a big loop so that when case A has finished you go back up to the top and do it again.
2. That depends on whether you leave lttr alone between cases, or re-initialise it to a new array.

Right now you've got a pretty linear command flow. User comes in at the top, and flows down through the code until they fall out the bottom. You could keep a similar linear flow, and simply loop around back to the case. I always wanted switch statements to have a "continue" to allow you to do this... no such luck, though. Maybe when I'm king... Anyway, you'd have to use a while loop to do that. That would still be linear, but now the line's got a loop in it.

If you wanted a little more structure in your code, you could make the input be a method, and again you'd loop on calling out to the method and returning the input. Handle the input in main (or in another method) and then go back out to the method. That would again use a while loop, but you'd be using a more modular approach.

Later on, you'll maybe think about getting into an OO approach - that's totally unnecessary here, but it might be good practice for you.
How would you change this to have a separate class handle the input and output? What if you wanted to switch between a console input (like you have here) and maybe a GUI? What would be the same about those two options, and how would you capture that?

This article has been dead for over six months. Start a new discussion instead.