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);
}
}
}
}
}
}
Related
I'm using Java to create a program that takes in a CSV file and outputs an Arff file. Whenever the program runs it comes up catching the exception that No source has been specified. When I delete the try catch it comes with the following error and I am not sure why,
Exception in thread "main" java.io.IOException: No source has been specified
at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:867)
at CSVtoArff.Convert(CSVtoArff.java:10)
at CSVtoArff.main(CSVtoArff.java:23)
Below is the code for the program
import weka.core.Instances;
import weka.core.converters.CSVLoader;
import weka.core.converters.ArffSaver;
import java.io.File;
public class CSVtoArff {
public static void Convert(String input, String output) throws Exception {
try {
CSVLoader load = new CSVLoader();
load.setSource(new File(input));
Instances data = load.getDataSet();
ArffSaver save = new ArffSaver();
save.setInstances(data);
save.setFile(new File(output));
save.writeBatch();
System.out.println("File successfully converted");
}
catch (Exception e) {
System.out.println("Does not meet arff standards: " + e.getMessage());
}
}
public static void main(String[] args) throws Exception{
String input = "C:\\Users\\jason\\Desktop\\example.csv";
String output =" C:\\Users\\jason\\Desktop\\example.arff";
Convert(input, output);
}
}
Please try putting the files in C:\temp folder and change it to below and try.
Sometime windows security my be denying access to protected system folders.
Also there is an extra leading space in output file path. I have removed that.
public static void main(String[] args) throws Exception{
String input = "C:/temp/example.csv";
String output ="C:/temp/example.arff";
Convert(input, output);
}
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);
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I get an IOException error when trying to compile this code:
public class TextEditor extends JDialog implements ActionListener {
public TextEditor (File fich,Frame owner) throws IOException{
super(owner,true);
fichier=fich;
String langage="//fortran_regex";
cree_ihm(langage);
};
public String config( String langage ) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (langage));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while( ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
private void cree_ihm(String langage) throws IOException{
config(langage);
}
}
///////When calling main
import utils.TextEditor;
public class launch_editeur {
public static void main(String[] args) throws IOException{
TextEditor editeur=new TextEditor(null);
editeur.Affiche(true);
//editeur.setControlOn(false);
}
}
What is happening?
Am I using it wrongly? I think it may have to do with the functions I called (or maybe with the classes? )
Thx
From your comment:
I get this: launch_editeur.java:5: error: cannot find symbol public static void main(String[] args) throws IOException{ ^ symbol: class IOException location: class launch_editeur 1 error
That's not the same as an IOException error (which happens at runtime), that's a compile error saying it can't find the class IOException. You need to import the IOException in the classes that use it:
import java.io.IOException;
The reader.close() call in your finally block needs a try { } catch around it, as it also can throw an IOException
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.