Results 1 to 14 of 14
  1. #1
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Good Morning all! A little help? please

    Ok so I am new to Java...obviously.
    After plenty of frustration I finally got this to compile and run, however my while and if statements dont appear to actually be controlling anything. What am I missing? (also if there is a better way to attach code please let me know) problem areas highligted in red

    */begin of code

    import java.util.*;
    import java.text.*;

    public class ModifiedPowerApp {
    public static void main(String[] args) {

    //call method
    MarcoHeading.getHeading("Assignment 3");

    // display a welcome message
    System.out.println(" Welcome to the Exponents and Roots Table\n");

    // create the Scanner object
    Scanner sc = new Scanner(System.in);

    //begin while *doesnt seem to control anything int i = 1;

    while (i <=15 & i > 0) {

    // perform conversions until choice is 0 or greater than 15

    // get an int from the user
    System.out.print(" Enter a number between 1 and 15 to continue\n Or zero or any negative number to exit: ");
    int num = sc.nextInt();
    System.out.println();

    // define the table and append the header rows
    String table = "";
    table += " Exponents --------------------!\t Roots-----------------------!\n";
    table += " Number\tSquare\tCube\tFourth\tFifth\t\tCube\tSixth \tNinth\tTwelfth\n";
    table += " ======\t======\t====\t======\t=====\t\t======\t=== ==\t=====\t=======\n";

    // appends the rest of the rows to the table
    for (i = 1; i <= 15; i++) {
    int square = i * i;
    int cube = i * i * i;
    double fourth = Math.pow(i,4);
    double fifth = Math.pow(i,5);
    double rootcube = Math.pow(i,1.0/3.0);
    double rootsixth = Math.pow(i,1.0/6);
    double rootninth = Math.pow(i,1.0/9);
    double roottwelfth = Math.pow(i,1.0/12);
    //Format decimals

    DecimalFormat exponent = new DecimalFormat("#,##0");
    DecimalFormat root36 = new DecimalFormat("0.00");
    DecimalFormat root9 = new DecimalFormat("0.000");
    DecimalFormat root12 = new DecimalFormat("0.0000");


    table += " " + i + "\t" + exponent.format(square) + "\t" + exponent.format(cube) + "\t" + exponent.format(fourth) + "\t" + exponent.format(fifth) + "\t\t" + root36.format(rootcube) + "\t" + root36.format(rootsixth) + "\t" + root9.format(rootninth) + "\t" + root12.format(roottwelfth) + "\n";
    } // end FOR


    // display the table
    System.out.println(table);



    } //end WHILE
    if (i > 15) { System.out.println("\n *Error *\n You needed to enter a number between 1 and 15. Restart your program.\n");
    System.exit(1);
    } // end FOR
    else
    if (i <= 0)
    System.out.println("Thank you for using this program ~");
    //call date and time method
    MarcoHeading.getName();
    MarcoDate.printfDate();
    MarcoDate.printfTime();

    } // end main METHOD

    } // end PowerApp CLASS

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if there is a better way to attach code please let me know

    edit your post and surround your code in [code] and /code tags


    your while loop has a problem:
    Java Code:
    while (i <=15 & i > 0) {

    should be double ampersand e.g.
    Java Code:
    while (i <=15 && i>0) {
    try that and tell me of any other problem

    http://download.oracle.com/javase/tu...operators.html

  3. #3
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Thanks so much, that helps. However, let me explain a bit....
    Here is what it is supposed to do.
    It should continue to prompt for an entry as long as a number between 1 and 15 is enterd.
    If a 0 or negative number is entered it is supposed to exit.
    If a number greater than 15 is entered it is supposed to display my error messge.

    However when I run it, the user is prompted to enter a number between 1 and 15 OR a zero or negative number to exit. however no matter what number is entered it runs as if the number entered was 15.
    AND
    it displays the error message...
    Thanks so much for any insight.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    however no matter what number is entered it runs as if the number entered was 15.
    thats because your int "i" should be determined from user-input (System.in). however you have used the SAME i in your FOR-loop, and once the FOR-loop runs it leaves i at 15 by the end:

    Java Code:
    for (i = 1; i <= 15; i++)

    what you shouldve done is, use a local variable in the for loop like this:
    Java Code:
    for (int i=1; i<=15; i++)

    so that the "i" in the for-loop doesnt change the other "i" you've declared before your while loop.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i see that you've made a Scanner object already, but you haven't used it yet...

    instead of setting i=1; i should be set to i = sc.nextInt() or something along those lines (i'm not too familiar with Scanner)

    i've also just noticed you've done this:
    Java Code:
    int num = sc.nextInt();
    so your while loop should use "num", not "i" because num holds the users input

  6. #6
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Hmmm, ok thanks let me toy around with this a bit more and get back to you
    Thanks!

  7. #7
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok so I made some changes and it is working much better,
    with what seems like 1 small issue.
    When I run the code it displays the answer properly, continues to prompt properly, and even displays the error if the number is above 15 properly.
    *However it doesnt exit the program like it is supposed to when either a "0" or "any negative number" is entered. ? grrr
    Here is my new code
    Java Code:
    */
    
    import java.util.*;
    import java.text.*;
    
    public class ModifiedPowerApp	{
      public static void main(String[] args)	    {
    
    		//call method
    		MarcoHeading.getHeading("Assignment 3");
    
            // display a welcome message
            System.out.println("  Welcome to the Exponents and Roots Table\n");
    
            // create the Scanner object
            Scanner sc = new Scanner(System.in);
    
    		//begin while
    		int i = 1;
    
    		while (i <=15 && i>0)	{
    
            // perform conversions until choice is 0 or greater than 15
    
                // get an int from the user
                System.out.print("  Enter a number between 1 and 15 to continue\n  Or zero or any negative number to exit:     ");
                int num = sc.nextInt();
                System.out.println();
    
                            // define the table and append the header rows
    			            String table = "";
    			            table += "         Exponents --------------------!\t  Roots-----------------------!\n";
    			            table += " Number\tSquare\tCube\tFourth\tFifth\t\tCube\tSixth\tNinth\tTwelfth\n";
    			            table += " ======\t======\t====\t======\t=====\t\t======\t=====\t=====\t=======\n";
    
    			            // appends the rest of the rows to the table
    			            for (i = 1; i <= num; i++)	  {
    			                int square = i * i;
    			                int cube = i * i * i;
    			                double fourth = Math.pow(i,4);
    			                double fifth = Math.pow(i,5);
    			                double rootcube = Math.pow(i,1.0/3.0);
    			                double rootsixth = Math.pow(i,1.0/6);
    			                double rootninth = Math.pow(i,1.0/9);
    			                double roottwelfth = Math.pow(i,1.0/12);
    			                //Format decimals
    
    			                DecimalFormat exponent = new DecimalFormat("#,##0");
    			                DecimalFormat root36 = new DecimalFormat("0.00");
    			                DecimalFormat root9 = new DecimalFormat("0.000");
    			                DecimalFormat root12 = new DecimalFormat("0.0000");
    
    
    			                table += "  " + i + "\t" + exponent.format(square) + "\t" + exponent.format(cube) + "\t" + exponent.format(fourth) + "\t" + exponent.format(fifth) + "\t\t" + root36.format(rootcube) + "\t" + root36.format(rootsixth) + "\t" + root9.format(rootninth) + "\t" + root12.format(roottwelfth) + "\n";
                }	// end FOR
    
    
                // display the table
                System.out.println(table);
    
            } //end WHILE
            //Error IF statement
    
            if (i > 15) {
    		    System.out.println("\n  *Error *\n  You needed to enter a number between 1 and 15.  Restart your program.\n");
    			  System.exit(1);
    		}  // end FOR
    		//this never gets ran
    			else
    		   	if (i <= 0)
    		   	System.out.println("Thank you for using this program ~");
    		   	       //call date and time method
    		   	    MarcoHeading.getName();
    			    MarcoDate.printfDate();
           			MarcoDate.printfTime();
    
        }	// end main METHOD
    
    }	// end PowerApp CLASS

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    //this never gets ran
    			else
    		   	if (i <= 0)
    Simple error: you are missing curly-braces '{' which you need to start the else-bracket, like this:
    Java Code:
    if (true) {
        //do something
    } else if (something-else true) {
       //do something else
    } else {
       //do if anything else true
    }

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    and it will never exit the system because you are still doing this:
    Java Code:
    		int i = 1;
    
    		while (i <=15 && i>0)

    i.e. you are not running the program depending on user-input. you should use your "num" variable for this, while num <=15 && num > 0 { loop } otherwise exit.

  10. #10
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok let me try to incorporate what you are suggesting into my coding,
    However consider this: when I run the program if I enter a number larger than 15 it does work my code:
    Java Code:
     if (i > 15) {
    System.out.println("\n  *Error *\n  You needed to enter a number between 1 and 15.  Restart your program.\n");
    			  System.exit(1);
    		}//end IF >15 Error
    However It just doesnt run through (when I enter 0 or a negative number)
    *
    Java Code:
    		else if (i<=0) 	{
    System.out.println("Thank you for using this program ~");
    		   	       //call date and time method
    		   	    MarcoHeading.getName();
    			    MarcoDate.printfDate();
           			MarcoDate.printfTime();
    		}
    so that leads to me to think perhaps it is pulling some user-input? Im not really sure...Thanks so much for all your input!

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    when I run the program if I enter a number larger than 15 it does work my code

    try to follow the input path of the program. from the moment you do this:
    num = sc.nextInt()

    from then on, the only time "num" i.e. user-input is used is in this:
    Java Code:
    for (i = 1; i <= num; i++)

    which is good and makes part of your program work, but what i'm trying to say to you is, the reason your program appears to work when a number input is larger than 15 is because you told the program to run while "i" = 1 to 15, which means even if you input -1 or 1 or 100 that means that NUM = -1,1 or 100 respectively but the program is only checking values of "i" which always stays between 1 and 15, as it is not changed by the user, not checking for user input.

    you might be afraid to change your code because you don't want to mess it up, so just make another copy of it, and edit it as much as you need to.
    Last edited by ozzyman; 03-25-2011 at 11:24 PM.

  12. #12
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    I hear what you are saying ozzyman, and you are helping me think this out, I am very new in Java and trying to wrap my brain around it all. I am going to pound away at this for a few hours and may be back :) thanks again

  13. #13
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok I have made a lot of progress and feel I am starting to see the light at the end of the tunnel!
    So the program seems to run perfect.....
    Except....when a 0 or a negative number is entered, it does end the program like it should, AND when a number higher than 15 is entered it gives an error.
    However it still runs through the "for" statement for either scenario. (I hope that makes sense)
    Example: I enter 16 it should only display error message, however it first runs through my "for" statement calculating all the exponents and roots and then displays the error message.
    Here is my most current code:
    Java Code:
    */
    
    import java.util.*;
    import java.text.*;
    
    public class ModifiedPowerApp4	{
      public static void main(String[] args)	    {
    
    		//call method
    		MarcoHeading.getHeading("Assignment 3");
    
            // display a welcome message
            System.out.println("  Welcome to the Exponents and Roots Table\n");
    
            // create the Scanner object
            Scanner sc = new Scanner(System.in);
    
    		//begin while
    		int i = 1;
    		int num = 1;
    		while (num <=15 && num>0)	{
    
            // perform conversions until choice is 0 or greater than 15
    
                // get an int from the user
                System.out.print("  Enter a number between 1 and 15 to continue\n  Or zero or any negative number to exit:     ");
                num = sc.nextInt();
                System.out.println();
    
                            // define the table and append the header rows
    			            String table = "";
    			            table += "         Exponents --------------------!\t  Roots-----------------------!\n";
    			            table += " Number\tSquare\tCube\tFourth\tFifth\t\tCube\tSixth\tNinth\tTwelfth\n";
    			            table += " ======\t======\t====\t======\t=====\t\t======\t=====\t=====\t=======\n";
    
    			            // appends the rest of the rows to the table
    			            for (i = 1; i <= num; i++)	  {
    			                int square = i * i;
    			                int cube = i * i * i;
    			                double fourth = Math.pow(i,4);
    			                double fifth = Math.pow(i,5);
    			                double rootcube = Math.pow(i,1.0/3.0);
    			                double rootsixth = Math.pow(i,1.0/6);
    			                double rootninth = Math.pow(i,1.0/9);
    			                double roottwelfth = Math.pow(i,1.0/12);
    			                //Format decimals
    
    			                DecimalFormat exponent = new DecimalFormat("#,##0");
    			                DecimalFormat root36 = new DecimalFormat("0.00");
    			                DecimalFormat root9 = new DecimalFormat("0.000");
    			                DecimalFormat root12 = new DecimalFormat("0.0000");
    
    
    			                table += "  " + i + "\t" + exponent.format(square) + "\t" + exponent.format(cube) + "\t" + exponent.format(fourth) + "\t" + exponent.format(fifth) + "\t\t" + root36.format(rootcube) + "\t" + root36.format(rootsixth) + "\t" + root9.format(rootninth) + "\t" + root12.format(roottwelfth) + "\n";
                }	// end FOR
    
    
                // display the table
                System.out.println(table);
    
            } //end WHILE
            		if (num > 15) {
    				    System.out.println("\n  *Error *\n  You needed to enter a number between 1 and 15.  Restart your program.\n");
    					  System.exit(1);
    				}  // end FOR
    			   	else
    
    		if (num <=0) 	{
    		   	System.out.println ("  Thank you for using this program  ~" + "  " + MarcoHeading.getName());
    		   	       //call date and time method
    
    			    MarcoDate.printfDate();
           			MarcoDate.printfTime();
    		}
        }	// end main METHOD
    
    }	// end PowerApp CLASS
    Thanks so much I greatly appreciate it!

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    remove "int i=1" you dont need that anymore
    Java Code:
    		//begin while
    		int i = 1;
    		int num = 1;
    		while (num <=15 && num>0)
    The problem is your for-loop still runs once before the while loop is broken. As a security measure, you should change this:
    Java Code:
    for (i = 1; i <= num; i++)	  {

    to this:
    Java Code:
    //takes the lowest of the two numbers essentially making 15 the maximum number
    int minNum = Math.min(num,15);
    for (i = 1; i <= minNum; i++)	  {

    this way it wont go over 15. you could also put an IF-statement after user-input to stop the program from running if the number is over 15. if this is what you want to do, change the following code:
    Java Code:
                num = sc.nextInt();
                System.out.println();

    to this:
    Java Code:
                num = sc.nextInt();
                //if the num is less than 1 OR greater than 15
                if (num <= 0 || num > 15) {
                    //stops the while-loop
                    break;
                }
                System.out.println();

Similar Threads

  1. Good Morning
    By Morpheus in forum Introductions
    Replies: 0
    Last Post: 03-22-2011, 05:04 PM
  2. How Good Is J#?
    By Salamander in forum Other IDEs
    Replies: 5
    Last Post: 09-02-2010, 03:29 PM
  3. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 04:32 PM
  4. Good Day
    By Kinyo in forum Introductions
    Replies: 0
    Last Post: 03-15-2009, 01:07 AM
  5. Any Good e-book in JSF
    By felixtfelix in forum Web Frameworks
    Replies: 0
    Last Post: 05-08-2008, 06:18 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
  •