Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: stack project-runtime error

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: stack project-runtime error

    it gives runtime error
    Please copy and paste the full text of the error message here.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: stack project-runtime error

    FileNotFoundException: 8
    Is because of these lines:
    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.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: stack project-runtime error

    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?

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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?

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stack project-runtime error

    Quote Originally Posted by Norm View Post


    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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default 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.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stack project-runtime error

    do you know a link that can teach me how to do it?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: stack project-runtime error

    Sorry, I don't.
    Google is the only place I can suggest.

  15. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stack project-runtime error

    the last question: is the code correct for determining the sequence of letters?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,444
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: stack project-runtime error

    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

  17. #17
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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?).

Similar Threads

  1. ERROR using Runtime.getRuntime().exec("arp -a");
    By Runomante in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 4th, 2011, 05:09 PM
  2. Runtime Error
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 10th, 2011, 03:09 PM
  3. Runtime Error running in UVA
    By mathfxr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2010, 02:06 PM
  4. Error of "cannot access InToPost" in 3 and 5 code
    By jaysoncutie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 25th, 2009, 09:12 AM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM