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: Having trouble completing tester class!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Having trouble completing tester class!

    This is the tester class.

    import java.util.Scanner;
    public class NumbersTester
    {
    public static void main(String[] args)
    {
    String s = "";
    Scanner in = new Scanner(System.in);
    System.out.print("Enter digits: ");
    int n = in.nextInt();
    s = Integer.toString(n);
    System.out.println(countDigits(s));
    Numbers s = new Numbers(int n);
    }
    }



    This is my other class:

    import java.util.Scanner;

    public class Numbers
    {
    private int n;
    public Numbers()
    {
    int n = intn;
    }
    }
    public static int countDigits(String s)
    {
    int count = 0;

    for (int i = 0; i < s.length(); i++)
    {
    if (s.charAt(i) == '7')
    {
    count++;
    }
    }
    return count;
    }

    }

    I am not sure what i am missing and would really appreciate help.

    thank you!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble completing tester class!

    Quote Originally Posted by bankoscarpa View Post
    This is the tester class.

    import java.util.Scanner;
    public class NumbersTester
    {
     public static void main(String[] args)
        {
            String s = "";    
            Scanner in = new Scanner(System.in);
            System.out.print("Enter digits: ");
            int n = in.nextInt();
            s = Integer.toString(n);
            System.out.println(countDigits(s));
    		Numbers s = new Numbers(int n);
    	}
    }


    This is my other class:

    import java.util.Scanner;
     
    public class Numbers
    {
    private int n;
     public Numbers()
     {
    	int n = intn;
     }
        }
     public static int countDigits(String s) 
        {
            int count = 0;
     
            for (int i = 0; i < s.length(); i++)
            {
                if (s.charAt(i) == '7') 
                {
                    count++;
                }
            }
            return count;
        }
     
    }
    I am not sure what i am missing and would really appreciate help.

    thank you!
    System.out.println(countDigits(s));

    There's no method called countDigits(s) in that class with the main method.

    Try this

    Number n = new Number();

    System.out.println(n.countDigits(s));

    I also don't see an intn as a variable anywhere in that Number class.

    Also, it seems you have both a class variable n and a local variable n that's only visible inside your constructor.

    You don't pass it the variable type with a reference parameter, only a declaration.

    For instance, if you had a method

    public int method (String variable, int number)

    then you could call it with something like

    method("Bla bla bla", 10);

    You don't mention the param data types when calling a method.

    Also, in this case, since that method is a static method, you can get away with using the class name without having to even make an object.

    you could use, for instance

    System.out.println(Number.countDigits(s));

    However, that only works if the method is static.

    Otherwise, you'll get an annoying error saying:

    "non-static method [the method] cannot be referenced from a static context."
    Last edited by javapenguin; May 3rd, 2012 at 09:18 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    bankoscarpa (May 3rd, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble completing tester class!

    Hmm, i see what you mean and i just tried that in my code and it did not work. I am not sure what i am doing wrong.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble completing tester class!

    Also, it appears that you have an extra bracket after your Numbers constructor.

    Anyway, Numbers isn't passed an int as a param anyway.
    Last edited by javapenguin; May 4th, 2012 at 12:33 PM.

  6. The Following User Says Thank You to javapenguin For This Useful Post:

    bankoscarpa (May 4th, 2012)

Similar Threads

  1. Dog Tester Class
    By mwardjava92 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2012, 05:33 PM
  2. Replies: 3
    Last Post: April 13th, 2011, 04:30 PM
  3. problem completing beginners' gui tutorial
    By mechnik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2011, 04:53 PM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:56 AM