FIleNotFoundException - illegal start of expression - NetBeans - java

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.

Related

Calculating physico-chemical properties of amino acids in Biojava

I need to calculate the number and percentages of polar/non-polar, aliphatic/aromatic/heterocyclic amino acids in this protein sequence that I got from UNIPROT, using BioJava.
I have found in the BioJava tutorial how to read the Fasta files and implemented this code. But I have no ideas how to solve this problem.
If you have some ideas please help me.
Maybe there are some sources where I can check it.
This is the code.
package biojava.biojava_project;
import java.net.URL;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
public class BioSeq {
// Inserting the sequence from UNIPROT
public static ProteinSequence getSequenceForId(String uniProtId) throws Exception {
URL uniprotFasta = new URL(String.format("https://rest.uniprot.org/uniprotkb/P31574.fasta", uniProtId));
ProteinSequence seq = FastaReaderHelper.readFastaProteinSequence(uniprotFasta.openStream()).get(uniProtId);
System.out.printf("id : P31574", uniProtId, seq, System.getProperty("line.separator"), seq.getOriginalHeader());
System.out.println();
return seq;
}
public static void main(String[] args) {
try {
System.out.println(getSequenceForId("P31574"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I don't know if BioJava stores these properties anywhere. But it's easy to just list all the amino acids with their properties manually. Then iterate over the sequence and count those that satisfy the property. Here's an example for the polarity:
import java.io.InputStream;
import java.net.URL;
import java.util.Set;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
public class BioSeq {
public static void main(String[] args) throws Exception {
ProteinSequence seq = loadFromUniprot("P31574");
int polarCount = numberOfOccurrences(seq, /*Polar AAs:*/ Set.of("Y", "S", "T", "N", "Q", "C"));
System.out.println("% of polar AAs: " + ((double)polarCount)/seq.getLength());
}
public static ProteinSequence loadFromUniprot(String uniProtId) throws Exception {
URL uniprotFasta = new URL(String.format("https://rest.uniprot.org/uniprotkb/%s.fasta", uniProtId));
try (InputStream is = uniprotFasta.openStream()) {
return FastaReaderHelper.readFastaProteinSequence(is).get(uniProtId);
}
}
private static int numberOfOccurrences(ProteinSequence seq, Set<String> bases) {
int count = 0;
for (AminoAcidCompound aminoAcid : seq)
if(bases.contains(aminoAcid.getBase()))
count++;
return count;
}
}
PS: don't forget to close IO streams after you used them. In the example above I used try-with-resources syntax which automatically closes the InputStream.

Getting and error when using Scanner in Java

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);
}
}
}

Java - How do I call a class's method with a string?

I'm trying to use my input from the console to pick which class's main method I want to run.
package run;
import java.lang.reflect.Method;
import java.util.Scanner;
import testing.*;
public class run {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException{
Scanner input = new Scanner(System.in);
String name = "testing."+input.nextLine();
Class Program = Class.forName(name);
//Try 1
Program obj = new Program();
//Got error "Program cannot be resolved to a type" on program and program
//Try 2
Program.main();
//Got error "The method main() is undefined for the type Class" on main
//Try 3
Class.forName(name).main();
//Got error "The method main() is undefined for the type Class<capture#2-of ?>" on main
}
}
Class program = Class.forName(name);
program.getDeclaredMethod("main", String[].class).invoke(null, new Object[]{args});
provide your main method is public static void main(String[] args)
This problem can be solved easily using Reflection. Check out the code below:
package run;
import java.lang.reflect.Method;
import java.util.Scanner;
import testing.*;
public class run {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, NegativeArraySizeException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Scanner input = new Scanner(System.in);
String name = "testing."+input.nextLine();
Class program = Class.forName(name);
program.getMethod("main", String[].class).invoke(null, Array.newInstance(String.class, 0));
}
}

The output file value is null (0KB)

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);
}
}
}
}
}
}

error: cannot find symbol: class FileNotFoundException

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.

Categories

Resources