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
Related
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.
I am trying to use a basic scanner using https://www.compilejava.net/. Here is the code:
import java.lang.Math;
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Gimmie a value");
Scanner scan = new Scanner(System.in);
int v1 = scan.nextInt();
System.out.println(v1);
}
}
This is the output:
Gimmie a value
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at HelloWorld.main(HelloWorld.java:29)
I did try your code in Eclipse and is working, it looks like the crappy online ide is not supporting the Inputstream, that is why you get an exception...
try using a real IDE instead, or another online ide...
The problem is not with your code, but with the online tool. It does not simulate user input. I suggest using one of free desktop programming tools if you are into real programming, like Netbeans or Intellij Idea.
For the time being, you may try another online tool, like Ideone. Here is a link to your code pasted and adapted to Ideone (note that I had to drop the public modifier from the class). Click the "clone" link on the site if you want to work on the code.
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.
I'm brand new to java and have tried to get this to work for the last 48 hours and I'm about to give up.
I want to put the java applet on a website. Works fine only in eclipse. I tried many solutions already suggested on this site and none of them worked for me and just mucked up the code so I've reverted it to my original state. Could anyone pinpoint the problem? Thanks!
(code edited to reflect suggested answers)
package nameapp;
import java.util.*;
import java.io.*;
import java.applet.Applet
public class NameApp extends Applet{
public static void main(String[] args) throws IOException {
String name;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name? ");
name = reader.readLine();
if (name.equals("Mike")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I am totally awesome!!!");
}
else if (name.equals("Lisa")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm the prettiest gal in the world!");
}
else {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm just ok i guess...");
}
}
}
And html is...
<applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
please read this tutorial that's contains basic stuff about JApplet,
please use JApplet not Applet
carrefully read signed java applet restrictions
What Applets Can and Cannot Do
https://stackoverflow.com/tags/java-web-start/info
note with Java 1.6.025 comings another restrictions for JApplet, and these problems and possible workaround are detailed described on this forum by Andrew Thompson, but I lost link ...
It looks like you are writing an application rather than an applet. When you run it in eclipse, do you select Run As... and then select Java Application? Try running it as a Java Applet instead. You should see appletviewer pop up with no content in it.
The entry point for an applet is the init() method, not main(), and the graphics related method paint() is also usually overloaded; I haven't seen an applet that has access to standard in and out.
You might find the stackoverflow question here useful: Main vs init.
code="nameapp/NameApp.class"
Put it would also need to extend java.applet.Applet and generally be an applet.
Read the Applet tutorial - Instead of a public static void main(String[] args) method, an Applet needs public void init().
Also, with the code you have now, you'll just see a blank Applet - you'll have to have the Java Debug Console up to see anything printed by System.out.println(), and Applets can't access System.in to read input - instead you'll want to add some TextField components to your Applet and read and write the text with those.
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);
}
}
}
}