Eclipse obsolete methods on the stack - java

I'm using Eclipse for Java on my Macbook air, OS 10.9. I keep getting the error Obsolete Methods on the Stack, with a warning about how it may cause problems with the virtual machine when I run very basic programs that have no errors. I ran a program that just had the class and the main method and I got this error. After the error box, the bug referred to the main method, but I know the syntax is correct because I used Eclipse's main method.
import java.util.Scanner;
public class Dowhile {
public static void main(String[] args) {
/*Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int value = scanner.nextInt();
System.out.println(value);*/
/*do{
System.out.println("Enter a number ");
int value = scanner.nextInt();
}
while(value != 5);
System.out.println("Got 5");*/
}
}
Update:
I'm not getting the obsolete methods error now, just Exception in thread "main"... at line 5.

This error message indicates that you are doing hot code replace, and that
the frames on the stack no longer match the class files in the running VM.
Restarting your debug session/target VM should suffice.
Hot code replace (HCR) meaning:
It is a debugging technique whereby the Eclipse Java debugger transmits new class files over the debugging channel to another JVM.
read more.
Bugzilla
Dreamincode

I clicked on Terminate button and it no longer showed that warning message again.

Related

Error while taking input from the user. NoSuchElementException

import java.util.Scanner;
public class Main {
public static void main(String [] args) {
Scanner sc=new Scanner (System.in);
System.out.println("Enter any two numbers: ");
int a,b,c;
a=sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println("The sum = "+c);
}
}
I did the same program on VS Code - it ran without issue.
I used the following online IDEs:-
tutorialspoint.com (same error)
jdoodle.com (executed successfully)
onlinegdb.com (executed successfully)
programquiz.com (executed successfully)
online-java.com (executed successfully)
w3schools.com (same error)
interviewbit.com (same error)
I had to use an online IDE for my interview - I don't remember.
I have tried to understand the exception, but I cannot align my issue with what the exception is all about. I've read multiple stack overflow threads about this issue. Some people are saying, I should not use close() - but I haven't even used it! Others are giving solutions that are not related to my issue at all.
Please give me some directions or hint so that I can learn from this problem.
I think it is more on how the IDE is written, may be it is not giving you an interactive screen to give an input and expects you to do that separately.
Just checked this for w3schools.com and interviewbit.com where we have a separate section for giving the input and this code runs perfectly fine there.
See both the screen shots attached

Java Program not running; error box is blank?

sorry if this question was repeated earlier, but I couldnt seem to find the answer. I have a code:
import java.util.Scanner;
public class PracticeProblems {
public static void main(String[] args) {
int x;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
x= input.nextInt();
if(x%2 == 0) {
System.out.println(x%2==0);
}
else {
System.out.println(x%2==0);
}
}
}
Now I am not too worried if the code actually works or not (because this same problem has occurred when the code was perfectly functional) I am just as confused why it doesnt run.
Try running by right clicking on PracticeProblems.java, and choosing "Run As Application". It looks like it is trying to run ComputeArea, and can't find that class.
I had a similar problems too.
Eclipse > Projects > Clean
if its not working try to copy the class. Delete the Project and recreate it
I have faced similar problems many times.
To solve this problem save the program, close Eclipse, reopen it and run the program without any loss of the program.
It works fine.

System.out.println terminated

I'm still new to Java and so I'm not understanding where the termination error is coming from. The System.out.println in the main is not being printed out and it says:
< terminated, exit value: 0> C:\Program Files\Java\jre1.8.0_71\bin\javaw.exe (Jan 27, 2016, 1:22:17 PM)
Here is the main of my code.
public class BrainCenter {
public static void main(String[] args) {
System.out.println("Welcome to the Stock Center!");
System.out.println("Realtime Reports within the stock market");
System.out.println("");
Market mk = new Market(6000);
mk.printMarket();
System.out.println("Individual status listed below");
System.out.println("");
Buyers bu = new Buyers();
bu.buildRandomPortfolio(mk);
}
}
The first three lines are not being outputted and I'm not sure why. It appears as the rest of the code loads but then disappear and said to be terminated at the end. I'm using Eclipse IDE.
Any help would be greatly appreciated. Thank you in advance!
Try to use java.exe instead of javaw.exe. The first will wait until the program completes. It will also put all output the program generates directly in console. The last should be used when the program runs with its own GUI or simply in background. It will not wait until the program completes.
See Difference between java.exe and javaw.exe.
you can fix this problem by setting path variable in Environment. In my system I have copied path of bin as "C:\Program Files\Java\jdk1.8.0_25\bin" and saved.

error messages are displaying different in both eclipse and command prompt

Sub: error messages are displaying different in both eclipse and command prompt
//DataHidingDemo program
class Bank {
private static double balance = 1000;// Data Hiding
}
public class DataHidingDemo extends Bank {
public static void main(String[] args) {
System.out.println("Balance:" + balance);
}}
Case 1:
run the program from eclipse and observe below error message is displaying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Bank.balance is not visible
at oops.DataHidingDemo.main(DataHidingDemo.java:10)
Case 2:
run the same program from command prompt and observe below error message is displaying
D:\Java Programs_CMD>javac DataHidingDemo.java
DataHidingDemo.java:10: error: balance has private access in Bank
System.out.println("Balance:"+balance);
^
1 error
Observe both case 1 & 2 error messages; case 2 error message has meaningful.
Q).Do we have the way to display the same error message in eclipse too? (for this do we need to change any setting in eclipse) please help on this.
Eclipse has its own Java compiler, which is thus different from javac, and thus generates different error messages. AFAIK, no, it's not possible to make Eclipse use the javac compiler. Both NetBeans and IntelliJ IDEA use javac, though, so you might want to try thise IDEs instead.
Note that the error you got from Eclipse is a message generated when you tried running code that did not compile. Don't do that. If there are compilation errors listed, then fix them all before running. The compilation errors are listed in the "Problems" view and in the "Markers" view of Eclipse.

Scanner cannot be resolved to a type

I just installed Ubuntu 8.04 and I'm taking a course in Java so I figured why not install a IDE while I am installing it. So I pick my IDE of choice, Eclipse, and I make a very simple program, Hello World, to make sure everything is running smoothly. When I go to use Scanner for user input I get a very odd error:
My code:import java.util.Scanner;
class test {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("hi");
}
}
The output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Scanner cannot be resolved to a type
Scanner cannot be resolved to a type
at test.main(test.java:5)
The Scanner class is new in Java 5. I do not know what Hardy's default Java environment is, but it is not Sun's and therefore may be outdated.
I recommend installing the package sun-java6-jdk to get the most up-to-date version, then telling Eclipse to use it.
If you are using a version of Java before 1.5, java.util.Scanner doesn't exist.
Which version of the JDK is your Eclipse project set up to use?
Have a look at Project, Properties, Java Build Path -- look for the 'JRE System Library' entry, which should have a version number next to it.
It could also be that although you are have JDK 1.5 or higher, the project has some specific settings set that tell it to compile as 1.4. You can test this via Project >> Properties >> Java Compiler and ensure the "Compiler Compliance Level" is set to 1.5 or higher.
I know, It's quite a while since the question was posted. But the solution may still be of interest to anyone out there. It's actually quite simple...
Under Ubuntu you need to set the java compiler "javac" to use sun's jdk instead of any other alternative. The difference to some of the answers posted so far is that I am talking about javac NOT java. To do so fire up a shell and do the following:
As root or sudo type in at command line:
# update-alternatives --config javac
Locate the number pointing to sun's jdk, type in this number, and hit "ENTER".
You're done! From now on you can enjoy java.util.Scanner under Ubuntu.
System.out.println("Say thank you, Mr.");
Scanner scanner = java.util.Scanner(System.in);
String thanks = scanner.next();
System.out.println("Your welcome.");
You imported Scanner but you're not using it. You're using Scanner, which requires user inputs. You're trying to print out one thing, but you're exposing the your program to the fact that you are going to use your own input, so it decides to print "Hello World" after you give a user input. But since you are not deciding what the program will print, the system gets confused since it doesn't know what to print. You need something like int a=sc.nextInt(); or String b=sc.nextLine(); and then give your user input. But you said you want Hello World!, so Scanner is redundant.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input seconds: ");
int num = in.nextInt();
for (int i = 1; i <=num; i++) {
if(i%10==3)
{
System.out.println(i);
}
}
}
}

Categories

Resources