stack project-runtime error
hey guys, I wrote a code for my java class project. it is about stacks. you can see my code below. I don know why but it gives
runtime error. please help me out.
Code Java:
import java.util.*;
import java.io.*;
public class Test
{
private static char[] stack=new char[100000];
private static int stackPoint=-1;
public static void main(String[] args) throws Exception
{
System.out.println("Enter file name: ");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String file=in.readLine();
Scanner scan=new Scanner(new FileReader(file));
out:while (scan.hasNext())
{
String stg=scan.nextLine();
for(int i=0; i<stg.length(); i++)
{
if(stg.charAt(i)=='{' || stg.charAt(i)=='(' || stg.charAt(i)=='[')
{
push(stg.charAt(i));
}
else if(i!=stg.length()-1 && stg.charAt(i)=='/' && stg.charAt(i+1)=='*')
{
push('/');
i++;
}
else if(stg.charAt(i)=='}' || stg.charAt(i)==')' || stg.charAt(i)==']')
{
char c=pop();
if(c=='-')
break out;
if(stg.charAt(i)=='}' && c!='{' || stg.charAt(i)==')' && c!='(' || stg.charAt(i)==']' && c!='[')
{
push(c);
break out;
}
}
else if(i!=stg.length()-1 && stg.charAt(i)=='*' && stg.charAt(i+1)=='/')
{
char c=pop();
if(c=='-')
break out;
if(c!='/')
{
push(c);
break out;
}
}
}
}
if(stackPoint==-1)
{
System.out.println("YES");
}
else
System.out.println("NO");
}static void push(char c)
{
stackPoint++;
stack[stackPoint]=c;
}
static char pop()
{
stackPoint--;
if(stackPoint<=-2)
return '-';
return stack[stackPoint+1];
}
}
Re: stack project-runtime error
Quote:
it gives runtime error
Please copy and paste the full text of the error message here.
Re: stack project-runtime error
Enter file name:
8
Exception in thread "main" java.io.FileNotFoundException: 8 (Sistem belirtilen dosyayı bulamıyor)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Test.main(Test.java:13)
"8" is an example. whatever I write as an input, it gives me the same error.
Re: stack project-runtime error
Quote:
FileNotFoundException: 8
Is because of these lines:
Code :
String file=in.readLine();
Scanner scan=new Scanner(new FileReader(file));
What do you want the Scanner class to do?
You are asking it to read from a file whose name is in the file variable.
Is there a file named: 8
When the program asks this question:
Enter file name:
You need to type in the name of an existing file.
Re: stack project-runtime error
I want scanner to read the file. actually the stack is determining if the input is valid or not and push or pop that data to the stack. example:
{{([])}} is a legal sequence but ()[]/* is not.
Re: stack project-runtime error
Quote:
I want scanner to read the file.
Is 8 the name of the file? You need to enter the name of a file.
What is the problem with entering the name of an existing file when the program asks for it?
Re: stack project-runtime error
no the name of the file must be "Test. java", but it does not accept it , it does not accept any input
Re: stack project-runtime error
Is there a file named: Test.java in the current directory when you execute your program?
That is a strange name for a data file. It looks like the name of your java source file.
Why do you want the program to read your source file? This is very confusing.
Create a small text file named: Test.data and add to it the data you want your program to read.
Then enter: Test.data when the program asks you: Enter file name:
How are you executing your program?
Re: stack project-runtime error
Quote:
Originally Posted by
Norm
Create a small text file named: Test.data and add to it the data you want your program to read.
Then enter: Test.data when the program asks you: Enter file name:
I have no idead what you are talking about.
Re: stack project-runtime error
Do you know how to use an editor? Like NotePad or wordpad
Do you know how to enter data into an editor?
Do you know how to save what you typed into the editor to a file?
If you can NOT do the above, how did you ever try to write a computer program?
Forget about reading data from a file.
Put the data into the Scanner class constructor and read it from there:
Scanner scan=new Scanner("{{([])}}\n"); // use this data for testing your program
Re: stack project-runtime error
ok it compiles when I use Scanner scan=new Scanner("{{([])}}\n"); but how am I supposed to make it work when it is Scanner scan=new Scanner(new FileReader(file));
Re: stack project-runtime error
Learn how to create a data file.
Then learn where to put the data file so the program can find it.
Then give the name of the file to the program when it asks for it.
Re: stack project-runtime error
do you know a link that can teach me how to do it?
Re: stack project-runtime error
Sorry, I don't.
Google is the only place I can suggest.
Re: stack project-runtime error
the last question: is the code correct for determining the sequence of letters?
Re: stack project-runtime error
Quote:
is the code correct for determining the sequence of letters?
Does the code do what you want?
I do not know what your mean by "sequence of letters".
Are you referring to the output of a sort. For example if b, c, a were sorted the sequence would be a,b,c
Re: stack project-runtime error
For what it's worth, the posted code finds and opens a file called 'Test.java' if such a file exists in the current directory. What it does then is anybody's guess. From the look of it, I suspect the code was written by a C programmer or translated from C to Java (how often do you see a 'break to label' construct in well-structured OO code?).