Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Need some help with this java program....

    I'm making this java program here using mutator,accessor and tostring method as well as other method that I don't even know what it's called..
    (I've just started java a couple of month ago ...)

    and yea, I seriously don't know what the error trying to say...
    help me please

    here's my coding
    Java Code:
    import java.util.Scanner;
    
    public class testbook
    {
    	public static void main (String[]args)
    	{
    		Scanner s = new Scanner(System.in);
    
    
    //Inputting how many books to save
    		System.out.println("Enter number of books : ");
    
    		int count = s.nextInt();
    
    		Book[]b = new Book[count];
    
    		for (int i = 0; i<count.length; i++)
    		{
    			System.out.println("Title of the book : ");					//input title
    
    			b[i].setTitle(title) = s.nextline();
    
    			System.out.println("Author of the book : ");				//input author
    
    			b[i].setAuthor(author) = s.nextline();
    
    			System.out.println("Publisher of the book : ");				//input publisher
    
    			b[i].setPublisher(publisher) = s.nextline();
    
    			System.out.println("ISBN of the book : ");					//input ISBN
    
    			b[i].setISBN(isbn) = s.nextDouble();
    
    			System.out.println("Price of the book : ");					//input price
    
    			b[i].setPrice(price) = s.nextDouble();
    
    			System.out.println("Year of publication of the book : ");	//input year of publication
    
    			b[i].setYear(year) = s.nextDouble();
    
    			System.out.println("Number of copies in the stock : ");		//input copies in the stock
    
    			b[i].setCopies(copies) = s.nextDouble();
    
    		}
    
    		int menu=0;
    
    		do
    		{
    			System.out.println("Main Menu\n1. Search book by Title\n2. Search by ISBN\n3. Update number of Copies of a Book\n4. Exit\n\n\nEnter the menu number (1 - 4) ");
    
    			menu = s.nextInt();
    
    			inMenu(menu,b);
    
    			toMenu();
    		}while (menu !=4);
    
    		System.out.println("Thank You for using this App. Have a nice day!");
    	}
    }
    
    
    public static void inMenu(int menu,Book[]b,)
    {
    	if (menu == 1)
    	{
    		System.out.println("Search book by Title\nEnter Title : ");
    
    		String searchTitle = s.nextline();
    
    		for (i=0;i<count.length;i++)
    		{
    			if (searchtitle.equals(b[i].title))
    			{
    				System.out.println(b[i].toString());
    
    			}
    		}
    	}
    	else if (menu == 2)
    	{
    		System.out.println("Search by ISBN\nEnter ISBN : ");
    
    		double searchISBN = s.nextDouble();
    
    		for (i=0;i<count.length;i++)
    		{
    			if (searchISBN.equals(b[i].isbn))
    			{
    				System.out.println(b[i].toString());
    			}
    		}
    	}
    	else if (menu == 3)
    	{
    		System.out.println("Update number of copies of a book\nEnter ISBN : ");
    
    		double searchISBN = s.nextDouble();
    
    		for (i=0;i<count.length;i++)
    		{
    			if (searchISBN.equals(b[i].isbn))
    			{
    				System.out.println ("Current copies in the stock is " +b[i].copies +"\nEnter new number of copies of this book : ");
    				b[i].setCopies(copies) = s.nextDouble();
    
    				System.out.println ("New info for book " +b[i].title + "\n" + b[i].toString());
    			}
    		}
    	}
    	else if (menu == 4)
    	{
    		System.out.println("Thank You for using this App. Have a nice day!");
    	}
    
    
    public static void toMenu()
    {
    	System.out.println("\n\nOthermenu (Y-Yes, N-No)? : ");
    
    	char main = s.next().charAt(0);
    
    	return main;
    }
    also, sorry if I posted on the wrong section... still new here

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Need some help with this java program....

    What is the error?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    got around uhh 20+ error here

    C:\Users\AreMint\Desktop\testbook.java:67: error: class, interface, or enum expected
    public static void inMenu(int menu,Book[]b,)
    ^
    C:\Users\AreMint\Desktop\testbook.java:73: error: class, interface, or enum expected
    String searchTitle = s.nextline();
    ^
    C:\Users\AreMint\Desktop\testbook.java:75: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:75: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:75: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:81: error: class, interface, or enum expected
    }
    ^
    C:\Users\AreMint\Desktop\testbook.java:88: error: class, interface, or enum expected
    double searchISBN = s.nextDouble();
    ^
    C:\Users\AreMint\Desktop\testbook.java:90: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:90: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:90: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:95: error: class, interface, or enum expected
    }
    ^
    C:\Users\AreMint\Desktop\testbook.java:102: error: class, interface, or enum expected
    double searchISBN = s.nextDouble();
    ^
    C:\Users\AreMint\Desktop\testbook.java:104: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:104: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:104: error: class, interface, or enum expected
    for (i=0;i<count.length;i++)
    ^
    C:\Users\AreMint\Desktop\testbook.java:109: error: class, interface, or enum expected
    b[i].setCopies(copies) = s.nextDouble();
    ^
    C:\Users\AreMint\Desktop\testbook.java:111: error: class, interface, or enum expected
    System.out.println ("New info for book " +b[i].title + "\n" + b[i].toString());
    ^
    C:\Users\AreMint\Desktop\testbook.java:112: error: class, interface, or enum expected
    }
    ^
    C:\Users\AreMint\Desktop\testbook.java:118: error: class, interface, or enum expected
    }
    ^
    C:\Users\AreMint\Desktop\testbook.java:122: error: class, interface, or enum expected
    public static void toMenu()
    ^
    C:\Users\AreMint\Desktop\testbook.java:126: error: class, interface, or enum expected
    char main = s.next().charAt(0);
    ^
    C:\Users\AreMint\Desktop\testbook.java:128: error: class, interface, or enum expected
    return main;
    ^
    C:\Users\AreMint\Desktop\testbook.java:129: error: class, interface, or enum expected
    }
    ^

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Need some help with this java program....

    Take your errors one at a time. For example, this line has an extra comma:

    Java Code:
    public static void inMenu(int menu,Book[]b,)
    Also, make sure all your opening curly brackets match to a closing curly bracket, and vice-versa.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    I got a hunch that this two is what making the main error

    Java Code:
    public static void inMenu(int menu,Book[]b)
    
    public static void toMenu()
    because the other error are the codes inside it and have the same error which is "error: class, interface, or enum expected"

    I'm confused @.@

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Need some help with this java program....

    Did you fix the two errors I told you about? If so, can you post your updated code and the new errors that it generates?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    already did, still the same error at those two parts I posted above and its content....

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need some help with this java program....

    Look carefully at your opening and closing braces. What is the closing brace on line 64 for? When you know that tell me where the inMenu method is?

  9. #9
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    closing brace on line 64 closes the "public class testbook" class,
    and the inMenu method is on the 67th line.

    what I'm trying to do is calling the inMenu method and bringing along the "menu" value inputted by user and also array "b" which is also inputted by the user to be used within the method, though honestly I don't even know if I can do that or the coding I used is true or not...

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Need some help with this java program....

    Quote Originally Posted by Mintz View Post
    closing brace on line 64 closes the "public class testbook" class,
    and the inMenu method is on the 67th line.
    Wouldn't that mean your inMenu() method is not inside any class? That's not allowed.

    Also, when do you *close* your inMenu() method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    I see, so the method needs to be in the class also...
    anyways, I've applied what u just said to it and I now got a whole 47 errors starting with the line 17 at the "count.length;" part...

    it says "error: int cannot be dereferenced"
    how should I fix this one .__.

  12. #12
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    ou wait, nevermind, just noticed that I can just use "count" instead of "count.length" .....

    anyways, the next few errors is from the line 21 to 45, (same error on the "b[i].set*Stuff* " part)

    the errors are "cannot find symbol"...

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need some help with this java program....

    When you get errors we need to see the exact messages like you posted above. You don't have to post them all just the first few. Sometimes when you fix one problem it can eliminate a whole bunch of others.

    "cannot find symbol" can mean two things: you are trying to call a method that does not exist or you are passing the wrong parameter list. You have declared a variable in one scope and are trying to access it a different scope. First work out which issue you have and the full error message will tell you.

  14. #14
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    I've been applying some fixes for the part I mentioned earlier and this is how it looks now
    Java Code:
    Book[]b = new Book[count];
    
            for (int i = 0; i < count; i++)
            {
                System.out.println("Title of the book : ");                 //input title
    
                b[i].setTitle(title) = s.nextLine();   //error
    
                System.out.println("Author of the book : ");                //input author
    
                b[i].setAuthor(author) = s.nextLine();   //error
    
                System.out.println("Publisher of the book : ");             //input publisher
    
                b[i].setPublisher(publisher) = s.nextLine();   //error
    
                System.out.println("ISBN of the book : ");                  //input ISBN
    
                b[i].setISBN(isbn) = s.nextDouble();   //error
    
                System.out.println("Price of the book : ");                 //input price
    
                b[i].setPrice(price) = s.nextDouble();   //error
    
                System.out.println("Year of publication of the book : ");   //input year of publication
    
                b[i].setYear(year) = s.nextDouble();   //error
    
                System.out.println("Number of copies in the stock : ");     //input copies in the stock
    
                b[i].setCopies(copies) = s.nextDouble();   //error
    
            }
    the error looks like
    Java Code:
    C:\Users\AreMint\Desktop\Java Mini Project\testBook.java:21: error: cannot find symbol
                b[i].setTitle(title) = s.nextLine();
                              ^

    here the other coding that have the mutator, accessor and toString method in case if the problem starts from here...

    Java Code:
    public class Book{
    
    	private String title;
    	private String author;
    	private String publisher;
    	private double isbn;
    	private double price;
    	private double copies;
    	private double year;
    
    		public Book()
    		{
    			title = "Java";
    			author = "Daniel";
    			publisher = "Pearson";
    			isbn = 112233;
    			price = 80.00;
    			year = 2014;
    			copies = 30;
    
    		}
    
    		public Book (String title, String author, String publisher, double isbn, double price, double year, double copies)
    		{
    			this.title = title;
    			this.author = author;
    			this.publisher = publisher;
    			this.isbn = isbn;
    			this.price = price;
    			this.year = year;
    			this.copies = copies;
    		}
    
    		public void setTitle(String title)
    		{
    			this.title = title;
    		}
    
    		public String getTitle()
    		{
    			return title;
    		}
            .
            .
            .
            .
            public void setCopies(double copies)
    		{
    			this.copies = copies;
    		}
    
    		public double getCopies()
    		{
    			return copies;
    		}
    
    		public String toString (){
    			return ("title: " + getTitle() + "\nauthor:" + getAuthor() + "\nPublisher:" + getPublisher() + "\nISBN:" + getISBN() + "\nPrice:" + getPrice() + "\nNumber of copies in the stock:" + getCopies() );
    		}
    	}

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default Re: Need some help with this java program....

    please gothru sun tutorial and start writing bulky programs.

    ur intention is ,u want to pass s.nextLine() to setTitle method right?


    u can do like this
    String input = s.nextLine();
    b[i].setTitle(input);
    Ramya:cool:

  16. #16
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: Need some help with this java program....

    I saw that one of the Sysout messages on your do-while conditional statement didn't end with a ); which would most likely result in one of the errors that you're having.

    I would also suggest using the Eclipse IDE, or some IDE that would show you the line that contains a possible error in it, so that you wont continue to write code and eventually, you have too many errors to know where you messed up.

  17. #17
    Matrice is offline Member
    Join Date
    Jul 2014
    Location
    Mililani
    Posts
    37
    Rep Power
    0

    Default Re: Need some help with this java program....

    Keep running the program. I hope you didn't just write this entire 130+ line of code in just one try and threw it in the compiler to let it spit out all of those errors. You would have caught some of them earlier on so that they would be easier to find and fix and you wouldn't be overwhelmed with all of those error messages.

    You're definitely a better programmer than me, I've only been programming for 2 in a half weeks so far. But we're all learning and mistakes are a definite occurrence.

  18. #18
    Mintz is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Need some help with this java program....

    thanks for everyone that spends their time helping me with this program
    I finally managed to completely resolve all the errors !! ~

    here's the new look of the whole program now, and with your advice on brace, I've keep track the braces I've used
    Java Code:
    import java.util.Scanner;
    
    public class testBook
    {
        public static void main (String[]args)
        {
            Scanner s = new Scanner(System.in);
    
    		System.out.println("Enter number of books : ");					//Inputting number of books to be saved
    
            int count = s.nextInt();
    
    		Book[]b = new Book[count];
    
            for (int i = 0; i < count; i++)
            {
                System.out.println("Title of the book : ");                 //input title
    
    			String title = s.nextLine();
    
                System.out.println("Author of the book : ");                //input author
    
    			String author = s.nextLine();
    
                System.out.println("Publisher of the book : ");             //input publisher
    
    			String publisher  = s.nextLine();
    
                System.out.println("ISBN of the book : ");                  //input ISBN
    
    			double isbn = s.nextDouble();
    
                System.out.println("Price of the book : ");                 //input price
    
    			double price  = s.nextDouble();
    
                System.out.println("Year of publication of the book : ");   //input year of publication
    
    			double year = s.nextDouble();
    
                System.out.println("Number of copies in the stock : ");     //input copies in the stock
    
    			double copies  = s.nextDouble();
    
    			 b[i].setTitle(title);
    			 b[i].setAuthor(author);
    			 b[i].setPublisher(publisher);
    			 b[i].setISBN(isbn);
    			 b[i].setPrice(price);
    			 b[i].setYear(year);
    			 b[i].setCopies(copies);
            }//close "for" loop
    
            int menu=0;
    
            do
            {
                System.out.println("Main Menu\n1. Search book by Title\n2. Search by ISBN\n3. Update number of Copies of a Book\n4. Exit\n\n\nEnter the menu number (1 - 4) ");
    
                menu = s.nextInt();
    
                inMenu(menu,count,b);
    
                toMenu();
            }while (menu !=4);
    
            System.out.println("Thank You for using this App. Have a nice day!");
        }//close "main" class
    
    
    	public static void inMenu(int menu,int count,Book[]b)
    	{
    		Scanner x = new Scanner(System.in);
    
        	if (menu == 1)
        	{
        	    System.out.println("Search book by Title\nEnter Title : ");
    
        	    String searchTitle = x.nextLine();
    
        	    for (int i=0; i < count; i++)
        	    {
        	        if (searchTitle.equals(b[i].getTitle()))
        	        {
        	            System.out.println(b[i].toString());
    
        	        }
        	    }
        	}
        	else if (menu == 2)
        	{
        	    System.out.println("Search by ISBN\nEnter ISBN : ");
    
        	    double searchISBN = x.nextDouble();
    
        	    for (int i=0;i < count ;i++)
        	    {
        	        if (searchISBN == b[i].getISBN())
        	        {
        	            System.out.println(b[i].toString());
        	        }
        	    }
        	}
        	else if (menu == 3)
        	{
        	    System.out.println("Update number of copies of a book\nEnter ISBN : ");
    
        	    double searchISBN = x.nextDouble();
    
        	    for (int i=0; i < count ; i++)
        	    {
        	        if (searchISBN == b[i].getISBN())
        	        {
        	            System.out.println ("Current copies in the stock is " +b[i].getCopies() +"\nEnter new number of copies of this book : ");
    
        	            double copies = x.nextDouble();
        	            b[i].setCopies(copies);
    
        	            System.out.println ("New info for book " +b[i].getTitle() + "\n" + b[i].toString());
        	        }
        	    }
        	}
        	else if (menu == 4)
        	{
        	    System.out.println("Thank You for using this App. Have a nice day!");
        	}
    	}//close "inMenu" class
    
    
    	public static char toMenu()
    	{
    		Scanner y = new Scanner(System.in);
    
    	    System.out.println("\n\nOthermenu (Y-Yes, N-No)? : ");
    
    	    char main = y.next().charAt(0);
    
    	    return main;
    	}//close "toMenu" class
    }
    but still, there's something that keeps me from running the application properly...

    Need some help with this java program....-testing-testbook.jpg


    I guess I need to dig in my coding again ... .__.

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Need some help with this java program....

    Try to figure it out yourself, but I'll just warn you: when I was learning Java I ran into -exactly- the problem you are having now and it took me a full day to realize my mistake. When you figure this one out, you are going to make a huge step forward to understanding how objects work.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default Re: Need some help with this java program....

    Quote Originally Posted by Mintz View Post
    thanks for everyone that spends their time helping me with this program
    I finally managed to completely resolve all the errors !! ~

    here's the new look of the whole program now, and with your advice on brace, I've keep track the braces I've used

    but still, there's something that keeps me from running the application properly...
    ...
    I guess I need to dig in my coding again ... .__.
    The NullPointerException means that an object was expected, but it found null instead. The error tells you where it is (Line 45):

    Java Code:
    b[i].setTitle(title);
    So on this line, something is null but it should be an existing object.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  3. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 04:13 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •