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 2 of 2

Thread: New to Java!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java!

    Hi everyone! I am taking some computer science classes, and have some questions for you guys:

    this is some homework that I have, but I cant seem to find some of the errors?


    // File: Errors.java

    // Purpose: A program with lots of syntax errors

    // Correct all of the errors (STUDY the program carefully!!)

    #import java.util.Scanner;

    public class errors

    {

    public static void main (String[] args)

    {

    String Name; / Name of the user

    int number;

    int numSq;

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter your name, please: ")

    Name = scan.nextInt();

    System.out.print ("What is your favorite number?);

    number = scan.nextInt();

    numSq = number * number;

    System.out.println (Name ", the square of your number is "

    numSquared);

    }




    AND




    /************************************************** *************

    //File: Paint.java

    //

    //Purpose: Determine how much paint is needed to paint the walls

    //of a room given its length, width, and height

    //************************************************** *************

    import java.util.Scanner;

    public class Paint

    {

    public static void main(String[] args)

    {

    final int COVERAGE = 350; //paint covers 350 sq ft/gal

    //declare integers length, width, and height;



    //declare double totalSqFt;



    //declare double paintNeeded;



    //declare and initialize Scanner object



    //Prompt for and read in the length of the room



    //Prompt for and read in the width of the room



    //Prompt for and read in the height of the room



    //Compute the total square feet to be painted--think

    //about the dimensions of each wall



    //Compute the amount of paint needed



    //Print the length, width, and height of the room and the

    //number of gallons of paint needed.

    }

    }



    Save this file to your directory and do the following:



    1. Fill in the missing statements (the comments tell you where to fill in) so that the program does what it is supposed to. Compile and run the program and correct any errors.

    2. Suppose the room has doors and windows that don't need painting. Ask the user to enter the number of doors and number of windows in the room, and adjust the total square feet to be painted accordingly. Assume that each door is 20 square feet and each window is 15 square feet.



    *****this is my first time using the scanner function so I am having some problems. I really dont even know where to start on the second problem. Thanks for your help!


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: New to Java!

    Well, we can't do your homework for you. Put your code in code tags so it is easier to read.

    Since your assignment is to find the errors, there is only so much help one can give.
    I will tell you that I see 7 issues:
    1) Import
    2) Commenting
    3) Semi-Colon
    4) Wrong Variable Assignment
    5) Unclosed String
    6) Wrong Concatenation Technique
    7) Illegally Breaking Up Statement (I think this is one)

    Do some research on those, and see if you can find these. There are possibly more, but that is a start.


    As for the second assignment, let's step you through it.
    It would seem your issue is your lack of experience with Scanner (Scanner (Java Platform SE 6)).
    The first thing to know about Scanner, is how to set up the inputting. For you, I think you are just using the console. So, to set the console as your input, you say:
    Scanner scan = new Scanner(System.in);
    Now, for the console, there are a few methods that may be helpful (all in their generic form). The most important method would be:
    Scanner.nextLine() //Returns a String of the User Input
    Here are the other possible useful methods:
    Scanner.next() //Returns a String of the next "token". A token is effectively a word
     
    Scanner.nextDouble() //Returns the next token as a Double, provided it can be converted
     
    Scanner.nextInt() //Returns the next token as a Int, provided it can be converted

    Have a look at the Scanner Class and do some research on the methods I stated above. Those are probably going to be the most helpful for you at this point.