Java Program not running; error box is blank? - java

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.

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

Eclipse obsolete methods on the stack

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.

Use Eclipse to Programmatically Track Execution Path

I know that Eclipse has powerful debugging capabilities. Are there any hooks that allow plug-ins to keep track of the code path followed when an open project is run?
For example, suppose I had the following program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
if(in.nextInt() == 1) {
num += 2;
} else {
num += 3;
}
System.out.println(num);
}
}
Is there some API that Eclipse exposes that would let me make a plug-in that determines which branch of the if statement this program took after it's executed once?
You might be interested in looking into code coverage. It's a metric used to determine what parts of your code have been run after execution. Typically, it's used for testing purposes, to see what parts of your code aren't run.
That said, you could use it to determine what branch was chosen by only running the program once. EclEmma, an eclipse plugin, will show you what branches were used by coloring the lines on the editor itself.

System.out.println not working

I am using eclipse for long time and this never happened but I have this really simple program . And it doesnt display anything . what can be the reason ?
public class ReportGenerator {
public static void main(String[] args){
System.out.println("STARTING");
}
}
try Window -> Show View -> Console if the console is not visible
If you are doing javac ReportGenerator.java, it is just building a .class file. You need to run java ReportGenerator to see the program working and therefore printing on the terminal in your case.
Too many opened consoles in Eclipse?
Here is the reference: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fconsole%2Fref-open_action.htm
Click the grey X until all of them are closed.

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