• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrong coding

 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can anybody tell me whats wrong with this as well as what is?
boolean
=false
=true
mean

Added Code tags, please read UseCodeTags
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the issue you are facing?

And

can be written as
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:can anybody tell me whats wrong with this as well as what is?

Well if you told us what the matter is it would help.

Second (and I'll say this before Campbell does)
if (d.snare == true) {
is redundant, and prone to error (although not incorrect logically). All you need is
if (d.snare) {

Winston

[Edit] Too slow
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the problem

C:\Users\Craig\java\src>javac Drumkit.java
.\Drumkit.java:1: duplicate class: DrumKit
class DrumKit {
^
.\Drumkit.java:14: duplicate class: DrumKitTestDrive
class DrumKitTestDrive {
^
Drumkit.java:17: cannot access Drumkit
bad class file: .\Drumkit.java
file does not contain class Drumkit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Drumkit d = new Drumkit();
^
3 errors

C:\Users\Craig\java\src>
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:This is the problem

C:\Users\Craig\java\src>javac Drumkit.java
.\Drumkit.java:1: duplicate class: DrumKit
class DrumKit {
^
.\Drumkit.java:14: duplicate class: DrumKitTestDrive

Well the message is quite explicit; although it may have something to do with the fact that your DrumKit class isn't public.

My suggestion: Add public to the definition, and remove the DrumKitTestDrive class altogether. It doesn't add anything, and since it's not public, the main() method will never be run anyway.

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that DrumKit is not the same as Drumkit. Java is case-sensitive.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Also note that DrumKit is not the same as Drumkit. Java is case-sensitive.

Well spotted that man. Totally missed it.

Winston
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys I have a new problem thanks for solving the last
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.


DrumKit.java:18: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^
DrumKit.java:23: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^

I also changed the coding
public class DrumKit {

boolean tophat = true;
boolean snare = true;

void playTopHat() {
System.out.println( "ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}

class DrumKitTestDrive {
public static void main(String [] args) {

2 errors

 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys ignore the last thing i said i solved it and have compiled it but i don't understand this

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main

This is what i did to the code on your suggestion i took drumkit testdrive



public class DrumKit {

boolean tophat = true;
boolean snare = true;

void playTopHat() {
System.out.println( "ding ding da-ding");
}
void playSnare() {
System.out.println("bang bang ba-bang");
}
}

 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:Hey Guys I have a new problem thanks for solving the last
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.


DrumKit.java:18: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^
DrumKit.java:23: cannot find symbol
symbol : method playsnare()
location: class DrumKit
d.playsnare();
^

I also changed the coding

2 errors



As already mentioned, Java is case-sensitive. This is also true for the method names.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:Sorry guys ignore the last thing i said i solved it and have compiled it but i don't understand this

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main

This is what i did to the code on your suggestion i took drumkit testdrive






The compiler is complaining that the DrumKit class doesn't have a main() method. Question. Does your DrumKit class have a main method?

Henry
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started and i am not sure what you mean
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what the main() method is? When you run a Java program, the Java runtime environment looks for a method public static void main(String[] args) in the class that you are trying to run.

You are trying to run class DrumKit, but it does not have a main() method. In fact, the main() method is in one of the other classes you wrote. You have to run that other class, not the DrumKit class.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You are trying to run class DrumKit, but it does not have a main() method. In fact, the main() method is in one of the other classes you wrote. You have to run that other class, not the DrumKit class.

Which is why I suggested that you remove the 'testDrive' class. It's not doing you any good. Just put the main() method in the DrumKit class, as Jesper says. In fact, while you're still in the early stages, my advice would be to stick to general rule of 1 public class per .java file (with the same name as the file too).

Winston
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right I changed my code to this



but now I have a new problem

C:\Users\Craig\java>java DrumKit
Exception in thread "main" java.lang.NoSuchMethodError: main



[Edit - added code tags - see UseCodeTags for details]
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code won't compile - you've declared the playTopHat and playSnare methods inside the main method.
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:That code won't compile - you've declared the playTopHat and playSnare methods inside the main method.



And looking at the error message, it looks like the new code changes wasn't compiled -- it is the same error from previous.

Henry
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what would I have to do to get out of main if you don't mind
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what would I have to do if you don't mind ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic