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

Thread: Java Program Help! Cant find errors

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Java Program Help! Cant find errors

    There is an error in this code, and i need help finding it as well as finding two ways on how to fix it.
     public class Test1 {
           public static void main(String[] args){ Stringy[] stringies = new
               Stringy[10]; for(int i = 0; i < stringies.length; i++){
    stringies[i] = new Stringy(); (stringies[i]).setString("I’m Stringy" +
    i); }
           } } class Stringy {
           private String name; Stringy(String name){ setString(name); }
           public void setString(String name){
        this.name = name; } }


    This is pretty much the same question but what is the output of the class

     public class Test2 { private static int counter = 0; private
           final int id = counter++; protected static int getCounter(){
               return this.counter; } public int getId(){ return this.id; }
           public static void main(String[] args){
               for(int i = 0; i < 100; i++){ System.out.println((new
                   Test2()).getId()); System.out.println((new
                   Test2()).getCounter());
    } }}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program Help! Cant find errors

    There is an error
    Can you explain why you think there is an error?
    Can you post something that shows what the error is and add some comments to the post describing what is wrong and what should happen?

    Please edit the code and format it. One statement per line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Java Program Help! Cant find errors

    In Test1 you have a constructor that takes a String. You call that constructor without the String input. And calling the function setString inside the constructor is redundant if you create an instance with a string name as a parameter.

    Looks like you could just delete the constructor in the Stringy class and it will work.

    In Test2 I have no idea what you are trying to do. What the output is, is what the output should be. What you expect is harder to say. You have the counter as a static field and are accessing it by creating a new instance, which adds another count to the counter.

    Anyway, you set the id to be = to the static field counter which is 0, then you add 1 to the counter. You print id which should be 0, Then you create an instance of Test which adds another count to counter, and print counter which should be 2 by now. Then you create another instance of Test which sets id to be = to counter or 2, and this adds another to counter which makes it 3, print id should be 2. Then you create another instance of Test which makes counter 4, print counter (4). Then create another instance of Test which makes counter = to 5, and sets id = to 4, print id. Create another instance and add another to counter, print counter which would be 6 by now... etc So the output should be

    0 set id to 0 print id or (0), then add 1 to counter
    2 create a new instance which adds 1 to counter to make 2, print counter or 2
    2 new instance which sets id to counter which is 2 print id which is 2, this new instance then adds 1 to counter to make 3
    4 new instance sets id to counter or 3, adds 1 to counter to make 4, print counter which is 4
    4 new instance sets id to counter (4), then add 1 to counter, print id (4) counter is now 5

    I think it would be better to ask how to write a piece of code that behaves a particular way.

Similar Threads

  1. Cannot find where extra brace is at causing my errors
    By eagle09 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 27th, 2011, 07:30 PM
  2. Cannot Find Variable/Symbol Errors... why?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2011, 04:12 AM
  3. [SOLVED] Could someone help me find my errors on this program?? (homework)
    By r19ecua in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 10:25 PM
  4. Replies: 3
    Last Post: February 23rd, 2011, 01:27 AM
  5. Help with Cannot Find Symbol Variable errors
    By skboone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 10:52 AM