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

Thread: java newbie help..

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default java newbie help..

    Eclipse keeps finding an error in the word Pet. It says "pet cannot be resolved to a type"
    Here is my code..THANKS!
    import java.util.Scanner;
     
    public class pets
    {
     
            public static void main (String[] args)
            {
     
            String readName1, readName2, readName3, readName4, readName5, n1, n2, n3, n4, n5;
     
            int readAge1, readAge2, readAge3, readAge4, readAge5, a1, a2, a3, a4, a5, avgAge;
     
            double readWeight1, readWeight2, readWeight3, readWeight4, readWeight5, w1, w2, w3, w4, w5, avgWeight;
     
     
     
    Scanner a = new Scanner(System.in);
            readName1 = a.nextLine();
            readAge1 = a.nextInt();
            readWeight1 = a.nextDouble();
            Pet pet1 = new Pet(readName1, readAge1, readWeight1);
            n1 = pet1.getName();
            a1 = pet1.getAge();
            w1 = pet1.getWeight();
     
     
    Scanner b = new Scanner(System.in);
            readName2 = b.nextLine();
            readAge2 = b.nextInt();
            readWeight2 = b.nextDouble();
            Pet pet2 = new Pet(readName2, readAge2, readWeight2);
            n2 = pet2.getName();
            a2 = pet2.getAge();
            w2 = pet2.getWeight();
     
     
     
    Scanner c = new Scanner(System.in);
            readName3 = c.nextLine();
            readAge3 = c.nextInt();
            readWeight3 = c.nextDouble();
            Pet pet3 = new Pet(readName3, readAge3, readWeight3);
            n3 = pet3.getName();
            a3 = pet3.getAge();
            w3 = pet3.getWeight();
     
     
    Scanner d = new Scanner(System.in);
            readName4 = d.nextLine();
            readAge4 = d.nextInt();
            readWeight4 = d.nextDouble();
            Pet pet4 = new Pet(readName4, readAge4, readWeight4);
            n4 = pet4.getName();
            a4 = pet4.getAge();
            w4 = pet4.getWeight();
     
     
     
    Scanner e = new Scanner(System.in);
            readName5 = e.nextLine();
            readAge5 = e.nextInt();
            readWeight5 = e.nextDouble();
            Pet pet5 = new Pet(readName5, readAge5, readWeight5);
            n5 = pet5.getName();
            a5 = pet5.getAge();
            w5 = pet5.getWeight();
     
     
     
    avgWeight = (w1+w2+w3+w4+w5)/5;
    avgAge = (a1+a2+a3+a4+a5)/5;
     
     
     
    if ((a1 < a2) && (a1 < a3) && (a1 < a4) && (a1 < a5))
                    {
                    System.out.println(n1 + " is youngest.");
                    } 
            if ((a2 < a1) && (a2 < a3) && (a2 < a4) && (a2 < a5))
                    {
                    System.out.println(n2 + " is  youngest.");
                    }
            if ((a3 < a1) && (a3 < a2) && (a3 < a4) && (a3 < a5))
                    {
                    System.out.println(n3 + " is youngest.");
                    }
            if ((a4 < a1) && (a4 < a2) && (a4 < a3) && (a4 < a5))
                    {
                    System.out.println(n4 + " is youngest.");
                    }
     if ((a5 > a1) && (a5 > a2) && (a5 > a3) && (a5 > a4))
                    {
                    System.out.println(n5 + " is youngest.");
                    }       
     if ((a1 > a2) && (a1 > a3) && (a1 > a4) && (a1 > a5))
                    {
                    System.out.println(n1 + " is the oldest.");
                    }
     if ((a2 > a1) && (a2 > a3) && (a2 > a4) && (a2 > a5))
                    {
                    System.out.println(n2 + " is the oldest.");
                    }
     if ((a3 > a1) && (a3 > a2) && (a3 > a4) && (a3 > a5))
                    {
                    System.out.println(n3 + " is the oldest.");
                    }
     if ((a4 > a1) && (a4 > a2) && (a4 > a3) && (a4 > a5))
                    {
                    System.out.println(n4 + " is the oldest.");
                    }
     if ((a5 > a1) && (a5 > a2) && (a5 > a3) && (a5 > a4))
                    {
                    System.out.println(n5 + " is the oldest.");
                    }  
     
     
            }
    }
    Last edited by helloworld922; November 30th, 2010 at 02:58 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java newbie help..

    woah, you have a ton of Scanner objects which all read in from System.in. You can declare one and simply use it again.

    You don't have a Pet class defined (computers are quite dumb and will have no idea or notion of what a Pet is without you specifically defining what it is). Remember, Java is case sensitive, as well as being letter sensitive (don't know quite how to describe this, but the names must match exactly).

    For example, the following 3 would all be considered different in Java:

    Pet
    Pets
    pet

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: java newbie help..

    First off, thanks for the quick reply. I understand the too many scanners part. Im in the process of fixing that. However, I dont understand the pet error I am receiving. Am I supposed to make another class called Pet? Or is there something I have to do it this class? We started this code in school and are supposed to finish it by tomorrow : /

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Red face Re: java newbie help..

    Quote Originally Posted by robin28 View Post
    First off, thanks for the quick reply. I understand the too many scanners part. Im in the process of fixing that. However, I dont understand the pet error I am receiving. Am I supposed to make another class called Pet? Or is there something I have to do it this class? We started this code in school and are supposed to finish it by tomorrow : /
    Your class is called pets

    You are attempting to do:

    Pet pet1 = new Pet(readName1, readAge1, readWeight1);

    It should be:

    pets pet1 = new pets(readName1, readAge1, readWeight1);

    But there will still be issues with readName1, readAge1, readWeight1

    You need to create a constructor of pets.

    For example:

            public pets(String readName1, int readAge1, double readWeight1) {
     
    	}
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. The Following 2 Users Say Thank You to JavaPF For This Useful Post:

    javapenguin (November 30th, 2010), robin28 (December 7th, 2010)

Similar Threads

  1. newbie start java program with switches
    By fortune2k in forum Java Theory & Questions
    Replies: 4
    Last Post: November 1st, 2010, 06:13 PM
  2. Java newbie...Help required
    By muraduk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 12th, 2010, 04:15 PM
  3. Java Newbie: How to Code Node and its Neighbors?
    By ke3pup in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:00 AM
  4. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  5. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM