Re: Illegal start of type?
Your try statement isn't inside any method or initializer block. Therefore it must either be a visibility modifier, abstract, variable type or return type.
Since it is not either of the first two, it must be either a variable type or a return type. However, try is already a reserved keyword, thus making it an illegal type name.
Try putting your try/catch code inside of a main function.
Re: Illegal start of type?
My original question has been answered, but have another one about the same program and wasn't sure if I should start a new thread or not.
Code Java:
import java.io.*;
import java.util.*;
class AminoToken
{
//System.out.println("**Inside AminoToken");
public static void main (String args[])
{
//System.out.println("**Inside main");
char inChar;
long aminosSize;
char aminosAry[];
String aminosStr = "";
StringTokenizer aminosLn;
StringTokenizer aminosInf;
File aminos;
FileInputStream aminosIS;
//System.out.println("**References created");
try
{
//System.out.println("**Inside try block");
aminos = new File("aminos.txt");
//System.out.println("**File created");
aminosIS = new FileInputStream(aminos);
//System.out.println("**FileInputStream created");
aminosSize = aminos.size();
//System.out.println("**aminosSize intialized");
aminosAry = new aminosAry[aminosSize];
//System.out.println("**aminosAry created");
for(int aminosAryInd = 0; aminosAryInd < aminosSize; aminosAryInd++)
{
aminosAry[aminosAryInd] = (char) aminosIS.read();
aminosStr += aminosAry[aminosAryInd];
System.out.print(aminosAry[aminosAryInd]);
}
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
}
catch (IOException ioe)
{
System.out.print(ioe);
}
System.out.println(aminosStr);
}
}
Now, my program compiles just fine, but it appears to hang when I run it. None, of the commented-out, double-asterisked print statements actually print, so I think it's safe to say that the program doesn't "do" anything meaningful,including terminate. I need to force-quit it from the command line.
Re: Illegal start of type?
How are you executing the program? I can't understand how the println()s output don't show on the console.
Can you show the commandline you use to execute the program?
Re: Illegal start of type?
When i copy your code and paste it into Netbeans, Netbeans gives errors at these parts:
Code Java:
aminosSize = aminos.size();
aminosAry = new aminosAry[aminosSize];
The errors say that;
It cannot find the .size in java.io.File
So, .size isn't in the java class java.io.File
The other error says that the
Code Java:
aminosAry = new aminosAry[aminosSize];
requires an int and finds a long
--
If i change the long aminosSize; to int aminosSize; , it will say that it can't find aminosAry
Code Java:
aminosAry = new aminosAry[aminosSize];
After Commenting the
Code Java:
aminosAry = new aminosAry[aminosSize];
out, i did get output :
Code :
**Inside try block
**File created
java.io.FileNotFoundException: aminos.txt (The system cannot find the file specified)
BUILD SUCCESSFUL (total time: 0 seconds)
--
Either way, it might be because i copied it :)
Re: Illegal start of type?
Try this:
Code Java:
package LoopPb;
import java.io.*;
import java.util.*;
class AminoToken
{
//System.out.println("**Inside AminoToken");
public static void main (String args[])
{
//System.out.println("**Inside main");
char inChar;
long aminosSize;
char aminosAry[];
String aminosStr = "";
StringTokenizer aminosLn;
StringTokenizer aminosInf;
File aminos;
FileInputStream aminosIS;
//System.out.println("**References created");
try
{
//System.out.println("**Inside try block");
aminos = new File("aminos.txt");
//System.out.println("**File created");
aminosIS = new FileInputStream(aminos);
//System.out.println("**FileInputStream created");
aminosSize = aminos.length();
//System.out.println("**aminosSize intialized");
aminosAry = new char[(int)aminosSize];
//System.out.println("**aminosAry created");
for(int aminosAryInd = 0; aminosAryInd < aminosSize; aminosAryInd++)
{
aminosAry[aminosAryInd] = (char) aminosIS.read();
aminosStr += aminosAry[aminosAryInd];
System.out.print(aminosAry[aminosAryInd]);
}
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
}
catch (IOException ioe)
{
System.out.print(ioe);
}
System.out.println(aminosStr);
}
}
You should parse file using while..loop, it's safer:
Code Java:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReaddinFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader f = null;
try {
f = new FileReader("aminos.txt");
int c;
while ((c = f.read()) != -1) {
System.out.print((char) c);
}
} catch (FileNotFoundException fnfe) {
System.out.println("fis abs");
fnfe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Read error");
ioe.printStackTrace();
} finally {
if (f != null) {
try {
f.close();
} catch (IOException ioe) {
System.out.println("Err inchidere fis");
ioe.printStackTrace();
}
}
}
}
}