Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: New java student need help with code

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default New java student need help with code

    Hi everyone this is my first post here and i need help with my java program. Im a freshman in college and joined in the middle of the year so im pretty far behind. My class also only meets once a week and my teacher is rather unavailable so can someone please help me with this program? The objective is to ask the user how many times they would like to see the figure X.


    import javax.swing.*; //Gives the windows where questions are asked

    public class MiniLab4 // class Name will begin with a Capital Letter.
    {
    public static void main( String[] args )
    {
    body();
    closingMessage();
    system.exit(0); <-- says right here ';' expected

    } // end main

    public static void closingMessage()
    {
    JOptionPane.showMessageDialog (null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
    }
    public static void openingMessage()
    {
    JOptionPane.showMessageDialog (null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);

    }//opening message

    public static void body()
    {
    openingMessage();
    getInt();
    }


    public static int getInt(String[] args)
    {
    int x;
    x = getInt ();


    System.out.println ("You entered " + x + ".");
    } // main

    public static int getInt()
    {
    int val;

    while (true) // a seemingly endless loop - uses break to get out
    { // loop until we get a valid int

    String s = JOptionPane.showInputDialog ( "Enter a whole number:" );

    if (s == null)
    {
    JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
    System.exit (0);
    } // cancel if
    else
    if (s.equals(""))
    {
    JOptionPane.showMessageDialog
    (null, "You must make an entry in the InputBox");

    } // entered nothing

    try
    {
    val = Integer.parseInt(s);

    break; // exit loop with valid int
    // could have had a variable change here to get out of loop

    }// try
    catch (Exception e)
    {
    JOptionPane.showMessageDialog(null, "Enter a whole number");

    }// catch
    while (number <= times)
    {
    xFig();
    System.out.println
    number = number++1; <-- Not a valid declaration
    }
    } // while

    return val; //return the number to the calling method

    } //end getInt


    public static void bottom()
    {
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" *******");
    System.out.println (" *****");
    System.out.println (" ***");


    }//top figure

    public static void top()
    {

    System.out.println (" ***");
    System.out.println (" *****");
    System.out.println (" *******");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");

    }// bottom figure

    public static void blank()
    {
    System.out.println ("");

    } // blank

    public static void xFig()
    {
    bottom();
    top();

    }
    }

    Whenever i try to run this program it never asks how many X's you would like to see
    Last edited by Darkcore123; October 24th, 2011 at 09:33 AM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: New java student need help with code

    It would help if you could tell us the current problem you're having with your code

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: New java student need help with code

    oh sorry xD, well whenever i run the program it doesnt even ask for the amount of x's you would like to see.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: New java student need help with code

    Theres a few things wrong with your code. I'll blacken the lines i modified so you understand:

    import javax.swing.*; //Gives the windows where questions are asked

    public class MiniLab4 // class Name will begin with a Capital Letter.
    {
    public static void main( String[] args )
    {
    body();
    closingMessage();
    System.exit(0); // <-- originally system.exit(0) (no mayus)

    } // end main

    public static void closingMessage()
    {
    JOptionPane.showMessageDialog (null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
    }
    public static void openingMessage()
    {
    JOptionPane.showMessageDialog (null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);

    }//opening message

    public static void body()
    {
    openingMessage();
    getInt();
    }


    public static int getInt(String[] args)
    {
    int x;
    x = getInt ();


    System.out.println ("You entered " + x + ".");
    } // main

    public static int getInt()
    {
    int val;

    while (true) // a seemingly endless loop - uses break to get out
    { // loop until we get a valid int

    String s = JOptionPane.showInputDialog ( "Enter a whole number:" );

    if (s == null)
    {
    JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
    System.exit (0);
    } // cancel if
    else
    if (s.equals(""))
    {
    JOptionPane.showMessageDialog
    (null, "You must make an entry in the InputBox");

    } // entered nothing

    try
    {
    val = Integer.parseInt(s);

    break; // exit loop with valid int
    // could have had a variable change here to get out of loop

    }// try
    catch (Exception e)
    {
    JOptionPane.showMessageDialog(null, "Enter a whole number");

    }// catch
    while (number <= times)
    {
    xFig();
    System.out.println("??"); // originally System.out.println no ";" and no parameter.
    number = number+1; // originally number = number++1;
    }
    } // while

    return val; //return the number to the calling method

    } //end getInt


    public static void bottom()
    {
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" *******");
    System.out.println (" *****");
    System.out.println (" ***");


    }//top figure

    public static void top()
    {

    System.out.println (" ***");
    System.out.println (" *****");
    System.out.println (" *******");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");
    System.out.println (" **** ****");

    }// bottom figure

    public static void blank()
    {
    System.out.println ("");

    } // blank

    public static void xFig()
    {
    bottom();
    top();

    }
    }

    * Also, the "public static int getInt(String[] args)" method doesn't return anything, so you might wanna change that to a "void" method
    * The variables "number" and "times" are not declared, do that.
    * At the "number = number++1;" part, if you're trying to increase the variable "number" by one, you can try using "number++;" instead of "number = number + 1". It's the same function, but i find that simpler ^^
    Keep me posted.
    Last edited by Henry518; October 24th, 2011 at 09:44 AM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: New java student need help with code

    I tried fixing the code, let me know if that's how it is supposed to work, i commented the lines i changed. The changes i made are the same i suggested before. Except for a loop that was in another loop (the one with "number <= times"... you'll see ):

    import javax.swing.*; //Gives the windows where questions are asked
     
    public class MiniLab4 // class Name will begin with a Capital Letter.
    {
     
        static int number = 0; //variables declared
        static int times = 0;
     
        public static void main(String[] args) {
            body();
            closingMessage();
            System.exit(0);
     
        } // end main
     
        public static void closingMessage() {
            JOptionPane.showMessageDialog(null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
        }
     
        public static void openingMessage() {
            JOptionPane.showMessageDialog(null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
     
        }//opening message
     
        public static void body() {
            openingMessage();
            getInt();
        }
     
        public static void getInt(String[] args) {
            int x;
            x = getInt();
            times = x; // times will be the number of times you want the figure to be printed. (Is reduntant as you can use "x" instead)
     
            System.out.println("You entered " + x + ".");
        } // main
     
        public static int getInt() {
    //Got rid of the variable "val". Using "times" instead.
     
            while (true) // a seemingly endless loop - uses break to get out
            { // loop until we get a valid int
     
                String s = JOptionPane.showInputDialog("Enter a whole number:");
     
                if (s == null) {
                    JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
                    System.exit(0);
                } // cancel if
                else if (s.equals("")) {
                    JOptionPane.showMessageDialog(null, "You must make an entry in the InputBox");
     
                } // entered nothing
     
                try {
                    times = Integer.parseInt(s);
     
                    break; // exit loop with valid int
    // could have had a variable change here to get out of loop
     
                }// try
                catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Enter a whole number");
     
                }// catch
            } // while
     
     
            while (number <= times) { // I took this loop out of the other loop above, the "break;" would've never let this loop occur
                xFig();
                number = number + 1;  // Got rid of the unused System.out.println();
            }
            return times; //return the number to the calling method
     
        } //end getInt
     
        public static void bottom() {
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" *******");
            System.out.println(" *****");
            System.out.println(" ***");
     
     
        }//top figure
     
        public static void top() {
     
            System.out.println(" ***");
            System.out.println(" *****");
            System.out.println(" *******");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
     
        }// bottom figure
     
        public static void blank() {
            System.out.println("");
     
        } // blank
     
        public static void xFig() {
            bottom();
            top();
     
        }
    }
    Last edited by Henry518; October 24th, 2011 at 11:04 AM.

  6. #6
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: New java student need help with code

    well first I would like to say well come to java programming.

    secondly can every one use the
     reply with quotes to see me

    third here is your code back with some line edits it does not work yet but if you see what i am doing
    use your output statements
     System.out.println("I got to this spot"); // semi colon =)

    then you should be able to finish nicely.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package junkcode;
     
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author William
     */
    public class MiniLab4 {// class Name will begin with a Capital Letter.
     
        public static void main(String[] args) {
            body();
            closingMessage();
     
            /******  you don't need this at all  ****/
    //system.exit(0); //<-- says right here ';' expected
        } // end main
     
        public static void closingMessage() {
            JOptionPane.showMessageDialog(null, "Good job, have a nice day!", "Exit window", JOptionPane.INFORMATION_MESSAGE);
        }
     
        public static void openingMessage() {
            JOptionPane.showMessageDialog(null, "Welcome to the X viewer", "Opening Window", JOptionPane.INFORMATION_MESSAGE);
     
        }//opening message
     
        public static void body() {
            openingMessage();
            getInt();
        }
     
        /**
         * what is the difference between getInt(String[] args) and getInt()   ???
         * @param args
         * @return
         */
        public static int getInt(String[] args) {
            int x;
            x = getInt();
     
     
            System.out.println("You entered " + x + ".");
    //missing return statement
            return x;
        } // main
     
        public static int getInt() {
            int val;
     
            //very bad way to start a while loop it forced use do while loop
            // do things then while its true continue it removed the break statement
     
            while (true) // a seemingly endless loop - uses break to get out
            { // loop until we get a valid int
     
                String s = JOptionPane.showInputDialog("Enter a whole number:");
     
                if (s == null) {
                    JOptionPane.showMessageDialog(null, "Leaving program"); // exit on cancel
                    System.exit(0);
                } // cancel if
                else if (s.equals("")) {
                    JOptionPane.showMessageDialog(null, "You must make an entry in the InputBox");
     
                } // entered nothing
     
                try {
                    val = Integer.parseInt(s);
     
                    break; // exit loop with valid int
    // could have had a variable change here to get out of loop
     
                }// try
                catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Enter a whole number");
     
                }// catch
     
     
    // well first we need to figure what number and times are then we can decide wheather we can
    // create a argument for it.
     
                //I want number to be 0 and times 1 then its always true
                int number = 0;
                int times = 1;
                while (number <= times) {
                    xFig();
                    System.out.println("Hey do I work??? ");//<--- finish this statement was --> System.out.println
     
                    number = number++  /* guess you don't like semi colons */ ;
     
     
    //        1;  <---- what is this??
    }
    } // while
     
    return val; //return the number to the calling method
     
        } //end getInt
     
        public static void bottom() {
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" *******");
            System.out.println(" *****");
            System.out.println(" ***");
     
     
        }//top figure
     
        public static void top() {
     
            System.out.println(" ***");
            System.out.println(" *****");
            System.out.println(" *******");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
            System.out.println(" **** ****");
     
        }// bottom figure
     
        public static void blank() {
            System.out.println("");
     
        } // blank
     
        public static void xFig() {
            bottom();
            top();
     
        }
    }

    good luck if you get stuck throw a reply
    Last edited by william; October 24th, 2011 at 10:50 AM. Reason: spelling error

Similar Threads

  1. Beauty School student taking JAVA? doesn't work out so well, NEED HELP!
    By tmihans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2011, 07:58 AM
  2. Student.java
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2011, 02:40 PM
  3. i'm a fresh NIIT java student
    By femoozer in forum Member Introductions
    Replies: 2
    Last Post: August 20th, 2011, 02:12 PM
  4. Hello to all!/Java Student.
    By Benjamin Cainan in forum Member Introductions
    Replies: 1
    Last Post: May 3rd, 2011, 09:48 AM
  5. Replies: 2
    Last Post: January 7th, 2011, 08:10 PM