Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 hours ago.
Improve this question
this is my second question, thankfully I resolved the first question I made. OK so this time I am having difficulty printing to the console this Main method and Duck class, I am supposed to print out "waddle waddle" and Quack into the console, I think its a problem with the refrence varible: any help will be greatly appreciated I alredy tried everything to get the code to run but the Java error said
"exit status 1;"cannot find symbol
String waddling = donald.waddle();
symbol: variable donald.
location: class Duck."
class Duck {
public String quack() {
return "*waddle waddle*";
}
#Override
public String toString() {
return "quack";
}
class Main {
public static void main(String[] args) {
class Duck {
public String waddle() {
return "*waddle waddle*";
String waddling = donald.waddle();
System.out.println(waddling);
}
}
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am just learning Java.
And I want to improve my code and answer the question below.
3.Inside the main() method of Simulator , create an instance of a "Cat" object, and invoke that object's run() method.
Does my code require additional information? I want to answer the above question and this is what I have so far:
class Simulator {
public static void main(String[] args) {
Cat c = new Cat();
System.out.println(c);
}
}
Am I correct? I found the second class returns as an random integer. Will that work to invoke the run method.
There are so many things wrong...
"Code snippets" are for Javascript. Your program is Java. Java <> Javascript :(
Your "Cat" object implements a thread. You use threads for "concurrency", to do things "in parallel". Does your "meow" really merit spawning off a thread?
What about member "1"? You declare it. You initialize it. And then you fail to use it for ANYTHING. Q: Why bother?
System.out.println(c) prints the "object"; it doesn't print anything "meaningful".
Suggested modifications:
public class Cat
{
private int i;
public void meow() {
System.out.println("Meowing: " + i);
}
public Cat(int i) {
this.i = i;
}
public static void main(String args[]) {
Cat cat = new Cat (1);
cat.meow();
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
import java.util.HashMap;
public class Library {
Library() {
}
public void getFinishedBooks(HashMap<String, Boolean> library) {
if(library.size() < 1)
System.out.println("Error! Library is empty.");
else {
for(String book : library.keySet()); {
if((library.get(book)) == true)
System.out.println(library.get(book));
}
}
}
public static void main(String[] args) {
HashMap<String, Boolean> myBooks = new HashMap<String, Boolean>();
myBooks.put("Road Down the Funnel", true);
myBooks.put("Rat: A Biology", false);
myBooks.put("TimeIn", true);
myBooks.put("3D Food Printing", false);
Library myLibrary = new Library();
myLibrary.getFinishedBooks(myBooks);
}
}
It's a basic library program. I can't seem to find what's causing the error...
Error message: "Error! Cannot find symbol if((library.get(book)) == true)
symbol: variable book
location: class Library
Remove the ; in the line
for(String book : library.keySet())
Its always better to use IDE when you start learning the language. Warnings in the IDE will help you solve the issue quickly.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
import java.util.*;
public class ass {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[]c={"sid","is","cool"};
List<String>list1=new ArrayList<String>();
for(String w:c){
list1.add(w);
}
String[]q={"is"};
List<String>list2=new ArrayList<String>();
for(String t:q){
list2.add(t);
}
EditList(list1,list2);
for(int i=0;i<list1.size();i++){
System.out.printf("%s ", list1.get(i));
}
}
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
if(l2.contains(it.next()));
it.remove();
}
}
}
In this programme I have two lists. I wanted to remove the items that are common in the first and second list from the first list and print it. I don't want a workaround or any other code suggestions. Can someone please explain why my code is not working?
I am following New Boston's tutorials.
Here:
if(l2.contains(it.next()));
it.remove();
That semicolon after if is a real statement.
Thus it.remove() happens always; like if ... that if not there!
Thus the real answer: always always always use
if (){
stuff
}
... even for single statements! Same for loops!
try
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
String current=it.next();
if(l2.contains(current)){
i1.remove(current); // assuming u wish to remove from l1
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
OK! I'm off to a flying STOP in my first java lesson. LOL
I'm getting this error:
# javac MyFirstApp.java
MyFirstApp.java:7: error: class, interface, or enum expected
}
^
1 error
From the below code.
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
}
And I'm pretty certain that I copied the example from the book exactly the way it was written. So where am I going wrong here?
Thanks
The compilation error exactly tells you where to look. MyFirstApp.java:7 implies there is something wrong at line 7. If you go through your code at line 7 there is an extra closing bracket. Remove this bracket and it would start compiling perfectly.
You have an extra closing brace }..
Try this..
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
There is an unpaired } in your code.
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
} // <-- Does not have a corresponding '{'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
In my main class I want to check if there was a change by using a boolean variable:
public class Main {
private boolean change = false;
public boolean getChange() {
return change;
}
public void setChange(boolean change) {
this.change = change;
}
private void method1() {
// some command
setChange(true);
method1();
}
If I want to get this boolean value in my second class, I always get returned "false", no matter if my method1 ran or not.
public class BoolTest {
Main m = new Main();
System.out.println(m.getChange()); // returns "false"
}
You must have two instances of Main. Use the same one. Example:
Main m = new Main();
System.out.println(m.getChange());
m.setChange(true);
System.out.println(m.getChange());
You probably want to share the same instance over multiple classes. Pass the instance to the other classes and use them as expected.