I have a very simple java script, where i have a scanner and want to type into the console. But nothing shows up on the console when I type in eclipse. I don't understand what's wrong.
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("yo");
}
}
You are not take any input.
Try to wait an input first:
String nextInput = input.nextLine();
System.out.println(nextInput);
Well you should use:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(input.next());
}
Now you will get what you will write in console
Related
I have an application that has stored various classes in string form.
For example, I might have this class:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
that is stored as a string in a database.
I would like to know whether this string contains a class that can compile successfully.
How would I go about doing that? Is there a package for this?
Create a text file and write the code to the file using Java:
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}");
myWriter.close();
Now run the file via
String[] args = new String[] {"/bin/bash", "-c", "your_command", "with", "args"};
Process proc = new ProcessBuilder(args).start();
You'll get to know if it compiles from its return value.
I am trying to pull an input from the user for this program to start on some more complicated stuff. However no matter what I try I get this error:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Main.main(Main.java:8)
I am running this code here:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner test = new Scanner(System.in);
String x = test.nextLine();
System.out.println(x);
}
}
Any help is greatly appreciated!
You have to check before if nextLine() exists. Try this:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner test = new Scanner(System.in);
while (test.hasNextLine()) {
string = test.nextLine();
System.out.println(string);
}
}
}
/*package whatever //do not write package name here */
import java.util.*;
class Nested {
public static void main (String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t-->0)
{
String input=in.nextLine();
//solve(input);
}
}
}
I am passing
1
1
as input but it is showing me runtime error...don't know why because its working fine on geeksforgeeks ide
Code Jam accept classname as Solution
That was the issue!
Thanks a lot everyone for helping!
I'm receiving the following error:
printfile.java:6: error: cannot find symbol
throws FileNotFoundException {
^
symbol: class FileNotFoundException
location: class printfile
for the following code:
import java.io.File;
import java.util.Scanner;
public class printfile {
public static void main(String[]args)
throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println (" What file are you looking for? ");
String searchedfile = keyboard.next();
File file = new File(searchedfile);
if (file.exists()) {
System.out.println(" Okay, the file exists... ");
System.out.print(" Do you want to print the contents of " + file + "?");
String response = keyboard.next();
if (response.startsWith("y")) {
Scanner filescan = new Scanner(file);
while (filescan.hasNext()) {
System.out.print(filescan.next());
}
}
else {
System.out.print(" Okay, Have a good day.");
}
}
}
}
How can this error be resolved?
To use a class that is not in the "scope" of your program, (i.e. FileNotFoundException), you have to:
Call it's fully qualified name:
// Note you have to do it for every reference.
public void methodA throws java.io.FileNotFoundException{...}
public void methodB throws java.io.FileNotFoundException{...}
OR
Import the class from it's package:
// After import you no longer have to fully qualify.
import java.io.FileNotFoundException;
...
public void methodA throws FileNotFoundException{...}
public void methodB throws FileNotFoundException{...}
Suggest also taking a look in this question, explains pretty much everything you might want to know about Java's acess control modifiers.
I get the following error message 'llegal start of expression' at line
throws FileNotFoundException
I have done some research but wasn't able to fix it. Can you help? Many thanks, zan
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
import static java.lang.System.out;
public class training{
public static void main(String[]args){
throws FileNotFoundException{
Scanner diskScanner = new Scanner(new File("occupancy"));
out.println("Room\tGuests");
for(int roomNum = 0; roomNum < 10; roomNum ++){
out.print(roomNum);
out.print("\t");
out.println(diskScanner.nextInt());
}
}
}
}
You shouldn't have a curly brace before the throws keyword :
public static void main(String[]args) throws FileNotFoundException {
^-- no curly brace
Note that a class should always start with an uppercase letter.
Improper use of throws.
public static void main(String[]args) throws FileNotFoundException
{
..
}
It is better to use try..catch
public static void main(String[]args)
{
try
{
..
}catch(FileNotFoundException ex)
{
//
}
}
Your syntax is wrong. Check your source and/or the language specification.