11 Replies - 524 Views - Last Post: 30 August 2015 - 05:21 PM Rate Topic: -----

#1 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 01:22 PM

Hey everyone,

Sorry for my messy and poorly put together code. This was just my first pass without being able to readin/test with the file. So it might annoy more experienced programmers.

The problem: I can't get the darn in.dat file to read in to the program. This should be the easy part, but I'm stuck. I tried coding the entire path in, such as "C:/users/myname/folder/file.

I tried messing with permissions. I tried a different compiler. I put the file everywhere: the project folder, src, here, there, everywhere. This could be just a blunder on my part, but I can't seem to figure out what is wrong.

Thanks.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 
import java.util.Stack;
import java.util.regex.Pattern; 
import java.io.FileReader;
import java.util.EmptyStackException;




public class postfixeval {
  public static void main(String [] args) throws FileNotFoundException
  {
    final Pattern CHARACTER = Pattern.compile("\\S.*?");  
    final Pattern UNSIGNED_DOUBLE = Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?");
    
    File file = new File("in.dat"); 
    System.out.println("This is a postfix expression calculator.\t It reads in files and evaluates");
    
    Stack <Double> stack = new Stack <Double>(); 
    String next; 
    
    double operand1; 
    double operand2;
    
    try{  
      Scanner scan = new Scanner(new FileReader(file));
      while(scan.hasNext())
      {    
      if(scan.hasNext(UNSIGNED_DOUBLE))
         {
       next = scan.findInLine(UNSIGNED_DOUBLE);
       stack.push(new Double(next));
        }
      else 
      {
       operand1 = stack.pop(); 
       operand2 = stack.pop(); 
       
       next = scan.findInLine(CHARACTER);
       switch(next.charAt(0))
         {
       case '+': stack.push(operand1 + operand2); 
        break;
       case '-': stack.push(operand1 - operand2); 
       break; 
       case '*': stack.push(operand1 * operand2);
       break; 
       case '/': stack.push(operand1 / operand2); 
       break; 
       case '^': stack.push( Math.pow(operand1,operand2)); 
       break; 
       case '#': stack.push(java.lang.Math.sqrt(operand1));
       stack.push(operand2); 
       break; 
         }
      }
       }
      scan.close();
    }
   
    catch(FileNotFoundException e)
    {
     System.out.print("No file found, dawg. (^.^). ");
 }
    try{
    System.out.println("The value of the file is" + stack.pop());
    }
    catch(EmptyStackException e)
    {
     System.out.print("\nEmpty stack, bro. (^.^) \n");
    }
 }
 }


Is This A Good Question/Topic? 0
  • +

Replies To: Reading in from a file (Tried almost everything I searched online)

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 03:11 PM

You're using the current directory

http://technojeeves....rectory-in-java
Was This Post Helpful? 1
  • +
  • -

#3 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 03:58 PM

Thanks, I've already run a current directory check and made sure I had the file in there. Still spinning m ywheels on this one.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:04 PM

Quote

I can't get the darn in.dat file to read in to the program.

What do you mean by that actually?
Was This Post Helpful? 0
  • +
  • -

#5 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:13 PM

Trying to use scanner to get lines of a postfix expression from a file. \

Scanner scan = new Scanner(new FileReader(file));
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:18 PM

You seem to be assuming that the app is not finding/reading the file(?) What makes you think that?
Was This Post Helpful? 0
  • +
  • -

#7 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:25 PM

Been giving me a FileNotFoundException all day.
Was This Post Helpful? 0
  • +
  • -

#8 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:28 PM

Quote

Thanks, I've already run a current directory check and made sure I had the file in there


Please show that in the code and the value of that directory
Was This Post Helpful? 0
  • +
  • -

#9 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:35 PM

This is the code with the directory search:



import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; 
import java.util.Stack;
import java.util.regex.Pattern; 
import java.io.FileReader;
import java.util.EmptyStackException;




public class PostFixEval {
  public static void main(String [] args) throws FileNotFoundException
  {
	System.out.printf("Current directory is %s\n", System.getProperty("user.dir"));
	
	final Pattern CHARACTER = Pattern.compile("\\S.*?");  
	final Pattern UNSIGNED_DOUBLE = Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?");
    File file = new File("C:/Users/Collin/workspace/Project1/in.dat"); 
    System.out.println("This is a postfix expression calculator.\nIt reads in files and evaluates");
    Stack <Double> stack = new Stack <Double>(); 
    String next; 
    double operand1; 
	double operand2;
    
    try{		
     	Scanner scan = new Scanner(new FileReader(file));
     	while(scan.hasNext())
     	{    
     	if(scan.hasNext(UNSIGNED_DOUBLE))
        	{
     		next = scan.findInLine(UNSIGNED_DOUBLE);
     		stack.push(new Double(next));
        }
     	else 
     	{
     		operand1 = stack.pop(); 
     		operand2 = stack.pop(); 
     		
     		next = scan.findInLine(CHARACTER);
     		switch(next.charAt(0))
     				{
     		case '+': stack.push(operand1 + operand2); 
     			break;
     		case '-': stack.push(operand1 - operand2); 
     		break; 
     		case '*': stack.push(operand1 * operand2);
     		break; 
     		case '/': stack.push(operand1 / operand2); 
     		break; 
     		case '^': stack.push( Math.pow(operand1,operand2)); 
     		break; 
     		case '#': stack.push(java.lang.Math.sqrt(operand1));
     		stack.push(operand2); 
     		break; 
     				}
     	}
       }
     	scan.close();
    }
   
    catch(FileNotFoundException e)
    {
    	System.out.print("No file found. ");
 }
    try{
    System.out.println("The value of the file is" + stack.pop());
    }
    catch(EmptyStackException e)
    {
    	System.out.print("\nEmpty stack.\n");
    }
 }
 }



Here is the output of the program:

Current directory is C:\Users\Collin\workspace\Project1
This is a postfix expression calculator.
It reads in files and evaluates
No file found.
Empty stack.
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 04:58 PM

Start|Run

Type in cmd.exe and hit return

Run the following command and then run the code again, then tell me what happens
echo "2 4 +" >C:\Users\Collin\workspace\Project1\in.dat

Was This Post Helpful? 0
  • +
  • -

#11 Collin.Gilbert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 28-March 15

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 05:09 PM

It created a in.dat file that seems to be readable by compiler. I don't know why it wouldn't read my other files.
Was This Post Helpful? 0
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Reading in from a file (Tried almost everything I searched online)

Posted 30 August 2015 - 05:21 PM

Quote

It created a in.dat file that seems to be readable by compiler.

It's the runtime that reads it, not the compiler.

Quote

I don't know why it wouldn't read my other files.

My guess is that you put it in the wrong place - I often find files in Eclipse look to be in a different location than they actually are
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1