Class or interface expected error, any ideas!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    Class or interface expected error, any ideas!

    I don't want you nice guys to waste your too much of your time, and confused me in the process.

    I am hoping you can explain it to me like I am 2 years old, that's it.

    Hoping to get a handle on why am I getting this error "Class or interface expected error"

    If you could point me to it it, ramble if you have to so I may get it, I think that'll help; of course if you want to just tell me the answer, know what I mean, hey I cannot stop you:-)

    But seriously now, I do not know why I cannot modify this simple code:

    Code:
    public class Book
    {
        //
        private String author;
    
        // …………….
        private String title;
        // …………….
        private Int pages;
    
    
    }
    
       /**
         * Create ……………………..
         * ……………………….
         * …………………………
         * BELOW IS OUR CONTRUCTOR METHOD
         * Line below is the line HIGHLIGHTED, in the program
        */
        [B]public Book(Sring bookAuthor, String bookTitle)[/B]    
    {
            author = bookAuthor;
            title = bookTitle;
                              }
        public Book(Int bookPages)
        {
            pages = bookPages;
    
                              }
    
        /**
         * Return ………………..
         * BELOW IS OUR ASSESSOR
         */
        public String getAuthor()
        {
            return Author;
        }
    
        /**
         * Return …………………..
         * …………………..
         */
        
        
        /**
         * ……………………….
         * BELOW IS OUR ASSESSOR
         */
        public String getTitle()
        {
            return Title;
        }
    
    /**
         * WE HOPE TO PRINT
         */
        public void printAuthor()
        {
            // Simulate the printing of an Author.
            System.out.println("##################");
            System.out.println("# The Author of this book is:");
            System.out.println("# Author");
            System.out.println("# " + author + " extraordinaire.");
            System.out.println("##################");
            System.out.println();
            
        }
    Anyway, when I run, and the error pops up, there's nothing I can do but stare at it. I am not frustrated yet, but I haven't got a sense why it would do this.

    I think as I said if you go so far as comparing apples to apples it should eventually sink in:-)

    Thanks for your help!
  • stack
    New Member
    • Sep 2007
    • 40

    #2
    Start by moving the curly brace from line 12 to line 70.
    The curly braces at lines 2 and 70 will now contain your code, that is,
    the code of the class Book. You will also need the main method to
    compile it.

    Also, when you declare variables, you must use small letters for the
    type. Example int (not Int). Only the String type needs a capital "S"
    like it is in your code.

    You have declared variables author and title but then you try
    to use them with different names (Author and Title).

    Should I go into more depth? :-)

    Cheers
    stack

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32668

      #3
      Dököll,

      You're too used to using a case-insensitive language. You need to get into the habit of worrying about the case of letters. This is not a bad habit even for the case-insensitive languages mind-you.

      Good luck.

      Comment

      • Dököll
        Recognized Expert Top Contributor
        • Nov 2006
        • 2379

        #4
        unbelievable!

        stack, NeoPa, thanks to you both. You know, Java did not seem to yell at me for adding Int and not int, typo without a doubt, what a moron. Must say tireness had something to do with it:-) Will give it another whirl, thanks again...

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          #5
          No syntax errors, code compiled!

          [CODE=JAVA]

          public class Book
          {
          // ……………….
          private String author;

          // …………….
          private String title;
          // …………….
          private int pages;

          private int total;


          /**
          * Create ……………………..
          * ……………………….
          * …………………………
          *
          *
          *
          *
          *
          *
          *
          * /**
          * Create a machine that issues tickets of the given price.
          */
          public Book(int bookPages)
          {
          pages = bookPages;
          total = 0;
          }






          /**
          * @Return The price of a ticket.
          */
          public int getPages()
          {
          return pages;
          }



          public Book(String bookAuthor, String bookTitle)
          {
          author = bookAuthor;
          title = bookTitle;
          }


          /**
          * Return ………………..
          * BELOW IS OUR ASSESSOR
          */
          public String getAuthor()
          {
          return author;
          }

          /**
          * Return …………………..
          * …………………..
          */


          /**
          * ……………………….
          * BELOW IS OUR ASSESSOR
          */
          public String getTitle()
          {
          return title;
          }



          /**
          * Receive an amount of money in cents from a customer.
          * Check that the amount is sensible.
          */
          public void jumpOnepage(int total)
          {
          if(total > 0) {
          pages = pages + total;
          }
          else {
          System.out.prin tln("Use a positive amount: " +
          total);


          }
          }
          public void printAuthor()
          {
          if(total >= pages) {

          // Simulate the printing of a ticket.
          System.out.prin tln("########## ########");
          System.out.prin tln("# The Author of this book is:");
          System.out.prin tln("# " + author + " extraordinaire. ");
          System.out.prin tln("########## ########");
          System.out.prin tln();


          // Update the total collected with the price.
          total = total + pages;

          }

          }

          public int getTotal()
          {
          return total;
          }
          }

          [/CODE]

          Thanks again, truly appreciate it. I am going to play around see what it'll do...In a bit!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Dököll
            You know, Java did not seem to yell at me for adding Int and not int, typo without a doubt, what a moron.
            It couldn't yell at you for that because maybe you hade defined Int as a non-public
            or nested or inner class somewhere further down the file. When it saw that right
            curly bracket all bets were off and only syntactic diagnostics were produced.

            kind regards,

            Jos

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              #7
              Originally posted by JosAH
              It couldn't yell at you for that because maybe you hade defined Int as a non-public
              or nested or inner class somewhere further down the file. When it saw that right
              curly bracket all bets were off and only syntactic diagnostics were produced.

              kind regards,

              Jos
              Cleaned it up a little bit:

              [CODE=JAVA]

              public class Book
              {
              // ……………….
              private String author;
              // …………….
              private String title;
              // …………….
              private int pages;
              // …………….
              private int fetchedpages;
              // …………….
              private int balance;
              // …………….
              private int total;


              /**
              * Create ……………………..
              * ……………………….
              * …………………………
              *
              */

              /**
              * Create...
              */
              public Book(int bookPages)
              {
              pages = bookPages;
              balance = 0;
              total = 0;
              }






              /**
              * @Return....
              */
              public int getPages()
              {
              return pages;
              }







              /**
              * Return number of pages already inserted...
              */
              public int getBalance()
              {
              return balance;
              }







              public Book(String bookAuthor, String bookTitle)
              {
              author = bookAuthor;
              title = bookTitle;
              }





              /**
              * Check...
              */
              public void addAuthor(Strin g author)
              {
              if(author != null ) {
              author = author + author;
              }
              else {
              System.out.prin tln("Use a positive number: " +
              author);
              }
              }


              /**
              * Check...
              */
              public void addTitle(String title)
              {
              if(title != null) {
              title = title + title;
              }
              else {
              System.out.prin tln("Use a positive number: " +
              title);
              }
              }




              /**
              * Return ………………..
              * BELOW IS OUR ASSESSOR
              */
              public String getAuthor()
              {
              return author;
              }

              /**
              * Return …………………..
              * …………………..
              */


              /**
              * ……………………….
              * BELOW IS OUR ASSESSOR
              */
              public String getTitle()
              {
              return title;
              }


              public void addpages(int pages)
              {
              if(pages > 0) {
              balance = balance + pages;
              }
              else {
              System.out.prin tln("Use a positive number: " +
              pages);


              }
              }


              public void guessnumberofPa ges(int fetchedpages)
              {
              if(fetchedpages <= pages) {
              pages = pages - fetchedpages;
              }
              else {
              System.out.prin tln("The number of pages exceeded " + pages + " pages: ");
              System.out.prin tln("Where " + pages + " is the value contained within this book.");
              }
              }



              public void printAuthor()
              {
              if(total >= pages) {

              // ............
              System.out.prin tln("########## ########");
              System.out.prin tln("# The Author of this book is:");
              System.out.prin tln("# " + author + ".");
              System.out.prin tln("# " + "Author extraordinaire of " + title + ".");
              System.out.prin tln("# " + "There are " + pages + " pages " + " in this book " + ".");
              System.out.prin tln("########## ########");
              System.out.prin tln();


              // ........

              total = total + pages;

              }

              }

              public int getTotal()
              {
              return total;
              }
              }

              [/CODE]

              The only thing that isnt working is printing to system, I get:

              ############### ###
              # The Author of this book is:
              # Gigi.
              # Author extraordinaire of Bibi.
              # There are 0 pages in this book .
              ############### ###

              But there are pages included, the code demands that a user inserts pages which must be then found when called.

              Above code is by no means professional and I attempt to comment, please tell me why you think I cannot print the nuber of pages inserted when code is run.

              You can go in detail if you want, already submitted the work. Just trying to see what I can make it do:

              [CODE=JAVA]


              /**
              *from here, were good, I can add pages and see that they're there through *balance.
              */


              public void addpages(int pages)
              {
              if(pages > 0) {
              balance = balance + pages;
              }
              else {
              System.out.prin tln("Use a positive number: " +
              pages);


              }
              }




              /**
              *I am using this in a totally different way than what was asked here with this bit of code.

              *This was to calculate discounted price for a ticket. I hope to make be used for user to guess number of pages in a book.

              */



              public void guessnumberofPa ges(int fetchedpages)
              {
              if(fetchedpages <= pages) {
              pages = pages - fetchedpages;
              }
              else {
              System.out.prin tln("The number of pages exceeded " + pages + " pages: ");
              System.out.prin tln("Where " + pages + " is the value contained within this book.");
              }
              }

              [/CODE]

              User must guess number of pages in the book. If incorrect will no but it will not equal to zero.

              By the time we exchange notes again I would have realized what's a miss but do throw a thought here if you would. In a bit!
              Last edited by Dököll; Oct 3 '07, 09:25 AM. Reason: comment formation

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                I stopped reading your code when I saw this:

                [code=java]
                public void addAuthor(Strin g author)
                {
                if(author != null ) {
                author = author + author;
                }
                else {
                System.out.prin tln("Use a positive number: " +
                author);
                }
                }
                [/code]

                I saw that 'pattern' a few times more ... what is it supposed to do?

                kind regards,

                Jos

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  #9
                  Originally posted by JosAH
                  I stopped reading your code when I saw this:

                  [code=java]
                  public void addAuthor(Strin g author)
                  {
                  if(author != null ) {
                  author = author + author;
                  }
                  else {
                  System.out.prin tln("Use a positive number: " +
                  author);
                  }
                  }
                  [/code]

                  I saw that 'pattern' a few times more ... what is it supposed to do?

                  kind regards,

                  Jos
                  In the original code it serves as adding additional monetary amounts for the price of a book which is then compared when user enters an amount that is less than the price of the book:

                  [CODE=JAVA]

                  This code works fine for the purpose it serves...


                  public void insertMoney(int amount)
                  {
                  if(amount > 0) {
                  balance = balance + amount;
                  }
                  else {
                  System.out.prin tln("Use a positive amount: " +
                  amount);
                  }
                  }

                  [/CODE]

                  I was hoping to use the code to insert an author into the program, which seems to be working, no errors thus far, but when author is fetched, nothing is returned.

                  Above code does return value, even though the program spits out crazy when nothing is found, at least the message "Use a positive amount: " + amount..." should pop up. Gotta do something 'bout that message, does not sound right. But the code should be able to do that right, add and author. I already have a method of print anything added! Thanks for you input, you're making me think:-)

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Dököll
                    In the original code it serves as adding additional monetary amounts for the price of a book which is then compared when user enters an amount that is less than the price of the book:

                    [CODE=JAVA]

                    This code works fine for the purpose it serves...


                    public void insertMoney(int amount)
                    {
                    if(amount > 0) {
                    balance = balance + amount;
                    }
                    else {
                    System.out.prin tln("Use a positive amount: " +
                    amount);
                    }
                    }

                    [/CODE]

                    I was hoping to use the code to insert an author into the program, which seems to be working, no errors thus far, but when author is fetched, nothing is returned.

                    Above code does return value, even though the program spits out crazy when nothing is found, at least the message "Use a positive amount: " + amount..." should pop up. Gotta do something 'bout that message, does not sound right. But the code should be able to do that right, add and author. I already have a method of print anything added! Thanks for you input, you're making me think:-)
                    So you are trying to port code that works for integers to work for Strings? The + operator has a different effect on Strings than it has on integers so you might need to think about that and make the neccessary adjustments.

                    Comment

                    • Dököll
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 2379

                      #11
                      Originally posted by r035198x
                      So you are trying to port code that works for integers to work for Strings? The + operator has a different effect on Strings than it has on integers so you might need to think about that and make the neccessary adjustments.
                      That indeed, consumed by everything else I forgot the simplest of things. Thanks much r035198x!

                      Comment

                      Working...