Results 1 to 13 of 13
  1. #1
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Hi and of course Help...

    Hi everybody;
    Im a noob to Java, taking Information Systems classes at the local state university, and as you may have guessed I need help, I did a search and there is something similar to this in your forum from 2009 but this is still a unique question.

    My assignment was to:

    write a do, while loop that prompts the user to enter an interger. Then have the code square the number and display it until the number is 0.

    the error Im getting says I am not declaring the variable (sum) but I have a feeling Im doing a lot more than that wrong. So it would be a great help if someone would just show me how its done, cause my course is online so I get no actual instruction.

    Here is my code; be gentle.

    import java.util.Scanner;

    public class Squaring {

    public static void main (String[] args) {
    Scanner input = new Scanner(System.in);

    //Keep Squaring until the input is 0
    do {

    System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
    double i = input.nextDouble();
    double sum;
    sum = Math.sqrt(i);

    System.out.println(sum);




    } while (sum > 0);

    } //class
    } //main

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

    Default Re: Hi and of course Help...

    So where in fact do you declare the sum variable? Usually a variable is declared by stating its type such as int or String or double, or whatever it's supposed to be, and then the variable name.

    Moving from intro forum to new to java forum.

  3. #3
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    well for the sake of humoring you I thought I declared it here

    double i = input.nextDouble();
    double sum;
    sum = Math.sqrt(i);

    but now I know Im wrong let me read a bit.

  4. #4
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    I can not figure it out at the moment. I do not know how to get my code to say that I want it to square the inputted number then declare that number as a variable, also I do not know why saying double sum + Math.sqrt(i); does not declare sum as a varible meaning whatever the square root of 'i' is equal to.
    it is one of those things where the answere is right in front of me but i cant see it. Help

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

    Default Re: Hi and of course Help...

    Post your latest code attempt, the one where you declare your variables (also please add [code] [/code] around your pasted code so it is readable), please post any and all error messages and any and all output. Let's reassess what you've got.

  6. #6
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    Okay error message : /tmp/jc_1954/Squaring.java:19: i is already defined in main(java.lang.String[])
    int i = input.nextInt(); //define the variable 'i' as whatever is enered by user ^
    1 error


    My code is:




    [code start] import java.util.Scanner;

    public class Squaring {

    public static void main (String[] args) {

    int i; //declaring variable 'i' to be defined later?
    double sum = Math.sqrt(i); //declaring variable sum as square root of i ?

    Scanner input = new Scanner(System.in);

    //Keep Squaring until the input is 0
    do {


    // Prompt user to enter an interger to be squared
    System.out.print("Enter a interger to be squared(the program exits if the value is 0):");

    int i = input.nextInt(); //define the variable 'i' as whatever is entered by user

    Math.sqrt(i); //I do not know if this squares i or ho to write it so it does.



    System.out.println(sum);


    } while (sum != 0);

    } //class
    } //main [code end]



    I tried to display my line of thought as clearly as possible with the comments and tried to tab clearly sorry its sloppy.

  7. #7
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    ya its hard to tell how the code is going to translate from my little rectangle up to the reply page: let me try the quote tags


    Java Code:
    import java.util.Scanner;
    
    	public class Squaring {  
    
    		public static void main (String[] args) { 
    			
    	                        int i;                       //declaring variable i to be defined later?
    				double sum = Math.sqrt(i);  //declaring variable sum as square root of i ?
    
                                        Scanner input = new Scanner(System.in);
    				
                 //Keep Squaring until the input is 0
    	do {	
    
    
    		// Prompt user to enter an interger to be squared 
    		System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
    	        	
    				int i = input.nextInt(); //define the variable 'i' as whatever is entered by user
                                    	
    					 Math.sqrt(i); //I do not know if this squares i or ho to write it so it does.
    	       		
                   		 
    		
    		 			System.out.println(sum);
    
    		
                    
                   
    			} while (sum != 0);
    				
    } //class
    } //main
    Last edited by Fubarable; 07-06-2012 at 07:00 PM. Reason: quote tags changed to code tags

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

    Default Re: Hi and of course Help...

    Um... I showed you how to use code tags in my post above. [code] [/code] The first tag goes at the start of your code block and the second tag, the one with the slash, /, at the start goes at the bottom of the block.
    Never mind, I've changed your quote tags (don't use these except for quotes please) to code tags above.

    this error:
    /tmp/jc_1954/Squaring.java:19: i is already defined in main(java.lang.String[])
    int i = input.nextInt(); //define the variable 'i' as whatever is enered by user ^

    is telling you that you are now declaring i *twice*. Just declare it once, that's it.

    Also, you'll want to improve your code indenting, and I recommend that you use only 3 spaces to indent and that you use it uniformly. Your random indentations make it very hard to read your code, and this makes it hard for us to understand it and to help you.
    Last edited by Fubarable; 07-06-2012 at 07:05 PM.

  9. #9
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    Yes you did but as you can see Im a little slow and just came out of the new members read before you post link.. whose the goose? I am

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

    Default Re: Hi and of course Help...

    Regarding your code which I've formatted below (2-3 spaces for indents usually works best):

    Java Code:
    import java.util.Scanner;
    
    public class Squaring {
       public static void main(String[] args) {
          int i; // (A)
          double sum = Math.sqrt(i); // (B)
          Scanner input = new Scanner(System.in);
          // Keep Squaring until the input is 0
          do {
             System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
             int i = input.nextInt(); // (C)
             Math.sqrt(i); // (D)
             System.out.println(sum);
          } while (sum != 0);
       } 
    }
    Please see commented lines above. Letters below correspond to the comment letters.
    (A): It's OK to declare i and sum here if you want, but if you do, don't re-declare them as you do on line (C)
    (B): No, don't try to do a calculation before the user has even entered any data.
    (C): see (A) above.
    (D): Math.sqrt(...) calculates the square root of a number. Check out the Math API to see what its methods do. There's a much simpler way of calculating a number's square -- multiplying the number by itself. Don't forget to assign the result to sum.

  11. #11
    squeek is offline Member
    Join Date
    Jul 2012
    Location
    New Mexico
    Posts
    7
    Rep Power
    0

    Default Re: Hi and of course Help...

    Java Code:
     import java.util.Scanner;
    
    public class Squaring {  
    
    public static void main (String[] args) { 
    
    	Scanner input = new Scanner(System.in);
    	//prompt user to enter a number to be squared
    		double sum;
    
    	//Keep Squaring until the input is 0
    		do {
    			System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
    	       		int a = input.nextInt();
    			System.out.print(Math.sqrt(a));
                   		sum = input.nextDouble();
    			
    		} while (sum !=0);
    
    } //class
    } //main
    I got it to compile like this,
    I will have to wait till I get home to be able to run it. Unless you can direct me to an online compiler that can do it all, I am very restricted as to what i can download here. Thank you for your help Ill be back later I forsee having problems in the future. Lightning just took out our power and we are running on generators time to go home early i guess.

    P.S. where do you get the pretty numbered programming platform with all the colors and what not?
    Last edited by squeek; 07-06-2012 at 08:31 PM. Reason: post script

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

    Default Re: Hi and of course Help...

    Quote Originally Posted by squeek View Post
    P.S. where do you get the pretty numbered programming platform with all the colors and what not?
    Same place you did.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Hi and of course Help...

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Posting Permissions

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