Results 1 to 13 of 13
  1. #1
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Need Help With Simple Input Output Program

    Hello,

    I'm making a simple program for school. Basically its just "If key 'a', is pressed, output this text on the screen. I'm very foreign to Java, but am somewhat familiar with C. Here is what I got so far. What else do I need and do I need to make a layout? I will be handing this into a teacher so I need it so she can just press on the program and this prompt will pop up "Type a letter". How would I write this in the program?

    Thanks

    Java Code:
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_a ) {
    } else if (e.getKeyCode() == KeyEvent.VK_b ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_c ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_d ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_e ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_f ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_g ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_h ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_i ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_j ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_k ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_l ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_m ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_n ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_o ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_p ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_q ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_r ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_s ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_t ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_u ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_v ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_w ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_x ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_y ) {
           System.out.println("");
    
    } else if (e.getKeyCode() == KeyEvent.VK_z ) {
           System.out.println("");
    
        }
    
        repaint();

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Need Help With Simple Input Output Program

    i would make a class that runs the whole thing, so when you void the main it calls the method in another class. (if that make sense to you)

    so id have the classes
    Application (runs the whole thing) - this will call the method in menu
    menu (easier to store things)
    keyInput(where the methods are)

    id make a scanner object in the menu class, then have a method that prints the list of all available options. and if the scanner input matches one of those inputs do this, or do this. if no match tell them there isnt a match (see below) also wrap the hole thing in a do while!

    Java Code:
    char ch;
    do{
                this.printMenu();  //make a method called printMenu() that just displays options for the user e.g. System.out.println("n - do this"); etc etc
                 String choice=in.next(); //scanner object make one of these
                ch=choice.charAt(0);
                switch (ch) {
                    case 'C': case 'c':
                   do something
                    break;
                    
                    case 'N': case 'n':
                    do something
                    
                    break;
    }
    
    } while ((ch !='Q')&(ch !='q'));
    do that make sense to you, im trying to explain it without just giving you the anwer

  3. #3
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    I think I understand. Don't I need the 'e.getKeyCode()' command or does the printmenu substitute for this?. Also do I need to make a layout for where I'll type in data and where it will output? If I need that, how would I do this? I'm familiar with robotics programming not really computer programming if you know what I mean. I'm still a little confused about the printMenu function. Did I do this correctly. Do you have an example? Sorry about my indenting, its all screwed up.

    Java Code:
    char ch;
    do{
              public static void printMenu() {
    System.out.println("Type a letter")
                 String choice=in.next(); //scanner object make one of these
                ch=choice.charAt(0);
                switch (ch) {
    case 'A': case 'a':
     		System.out.println("");
                    break;
    case 'B': case 'b':
     		System.out.println("");
                    break;
    case 'D': case 'd':
     		System.out.println("");
                    break;
    case 'E': case 'e':
     		System.out.println("");
                    break;
    case 'F': case 'f':
     		System.out.println("");
                    break;
    case 'G': case 'g':
     		System.out.println("");
                    break;
    case 'H': case 'h':
     		System.out.println("");
                    break;
    case 'I': case 'i':
     		System.out.println("");
                    break;
    case 'J': case 'j':
     		System.out.println("");
                    break;
    case 'K': case 'k':
     		System.out.println("");
                    break;
    case 'L': case 'l':
     		System.out.println("");
                    break;
    case 'M': case 'm':
     		System.out.println("");
                    break;
    case 'N': case 'n':
     		System.out.println("");
                    break;
    case 'O': case 'o':
     		System.out.println("");
                    break;
    case 'P': case 'p':
     		System.out.println("");
                    break;
    case 'Q': case 'q':
     		System.out.println("");
                    break;
    case 'R': case 'r':
     		System.out.println("");
                    break;
    
                    case 'S': case 's':
     		System.out.println("");
                    break;
                     
                    case 'T': case 't':
     System.out.println("");                 
                    break;
    
    case 'U': case 'u':
     		System.out.println("");
                    break;
    
    case 'V': case 'v':
     		System.out.println("");
                    break;
    case 'W': case 'w':
     		System.out.println("");
                    break;
    case 'X': case 'x':
     		System.out.println("");
                    break;
    case 'Y': case 'y':
     		System.out.println("");
                    break;
    case 'Z': case 'z':
     		System.out.println("");
                    break;
    
    }
     
    } while ((ch !='Q')&(ch !='q'));
    Last edited by Mastermime; 12-10-2012 at 05:41 AM.

  4. #4
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Can anyone answer the questions above?

  5. #5
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Monkeyjr, Don't I need the 'e.getKeyCode()' command or does the printmenu substitute for this?. Also do I need to make a layout for where I'll type in data and where it will output? If I need that, how would I do this? I'm familiar with robotics programming not really computer programming if you know what I mean. I'm still a little confused about the printMenu function. Did I do this correctly.

  6. #6
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Also, does anyone know how I'd to make an image pop up along with text. For example, when John Doe types the letter A this text "blahblahblahblahblahblahblahblahblahblah" along with this certain picture

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Need Help With Simple Input Output Program

    you dont need the e.get code because you are no longer using e, you are using a char switch look up info on the switch method

  8. #8
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Instead of having the user type a letter in, I decided I'm going to add buttons instead. For this I m going to use gridlayout to layout the buttons and just use jbutton function and addactionlistener to display text and picture. Would thiswork? Is there anything else I would need. Could someone post some example code?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help With Simple Input Output Program

    Quote Originally Posted by Mastermime View Post
    Instead of having the user type a letter in, I decided I'm going to add buttons instead. For this I m going to use gridlayout to layout the buttons and just use jbutton function and addactionlistener to display text and picture. Would thiswork?
    Yes, that could work.

    Is there anything else I would need.
    Hard to say until you try to create it.

    Could someone post some example code?
    Example of what? Of your assignment? No, that's not how it works here.

  10. #10
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Can someone verify that I did this correctly? when trying to complile, I'm getting an error 'java.lang.NoClassDefFoundError: apples/Apples'. Why is this? Thanks eveyone for your help.
    Class Apples
    Java Code:
    import javax.swing.JFrame;
    
     class  Apples{
        public static void main(String[] args) {
          gui go = new gui();
          go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          go.setSize(1000,1000);
          go.setVisible(true);
        }
    }
    Class gui
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    
    public class gui extends JFrame{
         private JButton A;
         private JButton B;
         private JButton C;
         private JButton D;
         private JButton E;
         private JButton F;
         private JButton G;
         private JButton H;
         private JButton I;
         private JButton J;
         private JButton K;
         private JButton L;
         private JButton M;
         private JButton N;
         private JButton O;
         private JButton P;
         private JButton Q;
         private JButton R;
         private JButton S;
         private JButton T;
         private JButton U;
         private JButton V;
         private JButton W;
         private JButton X;
         private JButton Y;
         private JButton Z;
        
         public gui(){
            super("APUSH ABCs");
            setLayout(new FlowLayout());
        
            A = new JButton("A");
            add(A);
            B = new JButton("B");
            add(B);
            C = new JButton("C");
            add(C);
            D = new JButton("D");
            add(D);
            E = new JButton("E");
            add(E);
            F = new JButton("F");
            add(F);
            G = new JButton("G");
            add(G);
            H = new JButton("H");
            add(H);
            I = new JButton("I");
            add(I);
            J = new JButton("J");
            add(J);
            K = new JButton("K");
            add(K);
            L = new JButton("L");
            add(L);
            M = new JButton("M");
            add(M);
            N = new JButton("N");
            add(N);
            O = new JButton("O");
            add(O);
            P = new JButton("P");
            add(P);
            Q = new JButton("Q");
            add(Q);
            R = new JButton("R");
            add(R);
            S = new JButton("S");
            add(S);
            T = new JButton("T");
            add(T);
            U = new JButton("U");
            add(U);
            V = new JButton("V");
            add(V);
            W = new JButton("W");
            add(W);
            X = new JButton("X");
            add(X);
            Y = new JButton("Y");
            add(Y);
            Z = new JButton("Z");
            add(Z);
                    
            HandlerClass handler = new HandlerClass();
            A.addActionListener(handler);
            B.addActionListener(handler);
            C.addActionListener(handler);
            D.addActionListener(handler);
            E.addActionListener(handler);
            F.addActionListener(handler);
            G.addActionListener(handler);
            H.addActionListener(handler);
            I.addActionListener(handler);
            J.addActionListener(handler);
            K.addActionListener(handler);
            L.addActionListener(handler);
            M.addActionListener(handler);
            N.addActionListener(handler);
            O.addActionListener(handler);
            P.addActionListener(handler);
            Q.addActionListener(handler);
            R.addActionListener(handler);
            S.addActionListener(handler);
            T.addActionListener(handler);
            U.addActionListener(handler);
            V.addActionListener(handler);
            W.addActionListener(handler);
            X.addActionListener(handler);
            Y.addActionListener(handler);
            Z.addActionListener(handler);
        }
          private class HandlerClass implements ActionListener{
      public void actionPerformed(ActionEvent event){
        JOptionPane.showMessageDialog(null, String.format("%S", event.getActionCommand())); 
      }
          }
          }

  11. #11
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    I figured it out. My main class was not properly configured in the property windows. I have another question though haha. Right now, its only displaying one message ("hi") for every single button. What code do I use to make it where it displays a different message when I press on a different button?

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    
    public class gui extends JFrame{
         private JButton A;
         private JButton B;
         private JButton C;
         private JButton D;
         private JButton E;
         private JButton F;
         private JButton G;
         private JButton H;
         private JButton I;
         private JButton J;
         private JButton K;
         private JButton L;
         private JButton M;
         private JButton N;
         private JButton O;
         private JButton P;
         private JButton Q;
         private JButton R;
         private JButton S;
         private JButton T;
         private JButton U;
         private JButton V;
         private JButton W;
         private JButton X;
         private JButton Y;
         private JButton Z;
        
         public gui(){
            super("APUSH ABCs");
            setLayout(new FlowLayout());
        
            A = new JButton("A");
            add(A);
            B = new JButton("B");
            add(B);
            C = new JButton("C");
            add(C);
            D = new JButton("D");
            add(D);
            E = new JButton("E");
            add(E);
            F = new JButton("F");
            add(F);
            G = new JButton("G");
            add(G);
            H = new JButton("H");
            add(H);
            I = new JButton("I");
            add(I);
            J = new JButton("J");
            add(J);
            K = new JButton("K");
            add(K);
            L = new JButton("L");
            add(L);
            M = new JButton("M");
            add(M);
            N = new JButton("N");
            add(N);
            O = new JButton("O");
            add(O);
            P = new JButton("P");
            add(P);
            Q = new JButton("Q");
            add(Q);
            R = new JButton("R");
            add(R);
            S = new JButton("S");
            add(S);
            T = new JButton("T");
            add(T);
            U = new JButton("U");
            add(U);
            V = new JButton("V");
            add(V);
            W = new JButton("W");
            add(W);
            X = new JButton("X");
            add(X);
            Y = new JButton("Y");
            add(Y);
            Z = new JButton("Z");
            add(Z);
                    
            HandlerClass handler = new HandlerClass();
            A.addActionListener(handler);
            B.addActionListener(handler);
            C.addActionListener(handler);
            D.addActionListener(handler);
            E.addActionListener(handler);
            F.addActionListener(handler);
            G.addActionListener(handler);
            H.addActionListener(handler);
            I.addActionListener(handler);
            J.addActionListener(handler);
            K.addActionListener(handler);
            L.addActionListener(handler);
            M.addActionListener(handler);
            N.addActionListener(handler);
            O.addActionListener(handler);
            P.addActionListener(handler);
            Q.addActionListener(handler);
            R.addActionListener(handler);
            S.addActionListener(handler);
            T.addActionListener(handler);
            U.addActionListener(handler);
            V.addActionListener(handler);
            W.addActionListener(handler);
            X.addActionListener(handler);
            Y.addActionListener(handler);
            Z.addActionListener(handler);
        }
          private class HandlerClass implements ActionListener{
      public void actionPerformed(ActionEvent event){
        JOptionPane.showMessageDialog(null, String.format("hi")); 
      }
          }
          }

  12. #12
    Mastermime is offline Member
    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Simple Input Output Program

    Please help. This is due Friday. I will be very grateful. I know it has something to do with the JOptionPane line of code, but I dont know what.
    Last edited by Mastermime; 12-18-2012 at 05:00 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need Help With Simple Input Output Program

    Q: What is available to you inside of the actionPerformed method to allow you to tell which JButton has been pressed?
    A: the ActionEvent parameter object. This object has methods that can return the button that was pressed, getSource(), and that can return the actionCommand String for the button (which *usually* is the same as the button's text), getActionCommand().

    I suggest that you print out the String returned by the getActionCommand() method to see for yourself exactly what I'm talking about. Note that I think that this information is all well described in the Swing button tutorial.

Similar Threads

  1. Input Output stream
    By kazuhiko in forum Networking
    Replies: 13
    Last Post: 03-15-2012, 05:43 PM
  2. Need help with input/output program error
    By stefan2892 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 08:57 PM
  3. Replies: 3
    Last Post: 08-07-2010, 06:28 AM
  4. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 06:06 PM
  5. Replies: 4
    Last Post: 10-02-2008, 11:05 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
  •