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);
}
}
}
Related
/*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 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
I'm trying to write file using using file writer but the value is null :
FileReader inCorpus2=new FileReader("output2.txt");
FileWriter outCorpus2=new FileWriter("Doc2(THE WANTED FILE).txt");
Scanner sc2=new Scanner(inCorpus2);
try{
while(sc2.hasNextLine()){
String tempLine=sc2.nextLine();
Scanner sc3=new Scanner(tempLine);
while(sc3.hasNext()){
String temp=sc3.next();
for(int i=0;i<UC.length;i++){
for(int j=0;j<temp.length();j++){
if(temp.charAt(j)==UC[i])temp=removeChar(temp,j);
}
}
And this is the error massage :
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at aya.SecondFilePreproc.main(SecondFilePreproc.java:25)
I hope i was clear , i tried my best .. help me please .
Not sure if it's help, need to see full code. Just added some exceptions.
import java.io.*;
import java.util.Scanner;
public static String removeChar(String x,int y){
return "something";
}
public class SomeClass {
public static void main(String[] args) throws FileNotFoundException, IOException {
try(FileReader inCorpus2=new FileReader("output2.txt");
FileWriter outCorpus2=new FileWriter("Doc2(THE WANTED FILE).txt");
Scanner sc2=new Scanner(inCorpus2)
){
while(sc2.hasNextLine()){
String tempLine=sc2.nextLine();
Scanner sc3=new Scanner(tempLine);
while(sc3.hasNext()){
String temp=sc3.next();
//just a guess what is UC
//char UC[]={'A','B','C'};
}
for(int i=0;i<UC.length;i++){
for(int j=0;j<temp.length();j++){
if(temp.charAt(j)==UC[i])temp=removeChar(temp,j);
}
}
}
}
}
}
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.