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);
}
}
}
This is my first post, so please let me know if something should be different.
I've been trying to create a method that finds the index of a search term in a two dimensional ArrayList. This is the code I came up with:
import java.util.List;
public class Searcher {
public static int Search(List<List<String>> csv, String term) throws TermNotFoundException{
if (csv.get(0).contains(term)) {
return csv.get(0).indexOf(term);
}
else {
throw new TermNotFoundException("Term not found");
}
}
The problem I have right now is that when I hardcode a search term that occurs in the ArrayList I'm looking at, it works perfectly. The problem occurs when I try to use the term variable as shown above.
the specific ArrayList I'm looking at ( csv.get(0) ) is as follows:
[datetime_UTC, E1A, E1B, E1C, E2A, E2B, E3A, E3B, E3C, E3D, E4A, G1A, G2A, G2C, Zon]
As such, if I hardcode in "E1A", It'll find it and return 1. This doesn't work if I call the function in the main method and filling in the same thing for the variable term.
Is there something I'm missing?
EDIT: To elaborate, I cannot disclose the full two dimensional array due to privacy reasons. I can, however, show you more info.
Some suggested to not search twice, so I have changed the code as follows:
import java.util.List;
public class Searcher {
public static int Search(List<List<String>> csv, String term) throws TermNotFoundException{
System.out.println(csv.get(0));
System.out.println(term);
int result = csv.get(0).indexOf(term);
if (result != -1){
return result;
}
else {
throw new TermNotFoundException("Term not found");
}
}
The same problem occurs. Included are some debugging lines, here is the output:
[datetime_UTC, E1A, E1B, E1C, E2A, E2B, E3A, E3B, E3C, E3D, E4A, G1A, G2A, G2C, Zon]
E1A
TermNotFoundException: Term not found
at Searcher.Search(Searcher.java:12)
at Main.main(Main.java:10)
if it is any help. this is where I'm calling the function from:
import java.io.FileNotFoundException;
import java.util.List;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
List<List<String>> csv = CSVReader.Read("standard_profiles.csv");
try {
System.out.println(Searcher.Search(csv, "E1A"));
} catch (TermNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
List<String> list=new ArrayList<>();
list.add("000");
List<List<String>> listList=new ArrayList<>();
listList.add(list);
System.out.println(Search(listList, "000"));;
}
The above code returns 0, I wasn't able to reproduce your problem. However it won't work if your term isn't on the first list as .get(0)means you are only searching the first List in your List<List>
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 have been working on an assignment for my first class in programming. I am working with NetBeans. I finished my project and it worked fine. I felt I could do better so I scraped it and started with a fresh slate. Now I am getting a message that says "No main class found" when i try to run it. But I have a main class. Here is some of the code:
package MyName;
import java.util.*;
import java.io.*;
public class MyName {
public static void main(String[] args)throws Exception {
//declare new file
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
//check to see if file exist, if it does then delete it
if (newFile.exists()) {
newFile.delete();
}
//used to create a file
System.setOut(new PrintStream(newFile));
//instance 1 of Guitar object
Guitar myGuitar = new Guitar();
On the this line there is an error message saying "Can not find symbol" only I have a class built already. here is the line:
Guitar myGuitar = new Guitar();
Here is a piece of my class:
public class Guitar {
boolean isPlaying = false;
boolean isTuned = false;
I understand i should have kept the code I had. I wanted to try again and now I am stuck. Any advice will be greatly appreciated.
in java, you shouldn't use throws for the main method, it is already expected.
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.