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 2 of 2

Thread: could anyone tell me why am I getting this error?

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default could anyone tell me why am I getting this error?

    Exception in thread "main" java.lang.NumberFormatException: For input string: "datafile.txt"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at postfixstack.Main.main(Main.java:27)
    Java Result: 1

    the file contains

    1 + 2 - 3

    I'm creating two stacks 1: operands, and 2: operators, and then displaying it

    line 27 in Main contains "oprand.push(Integer.parseInt(next));"


    package postfixstack;
     
    import java.util.*;
     
    public class Main {
     
        public static void main(String[] args)
        {
            Stack<String> optor = new Stack();
            Stack<Integer> oprand = new Stack();
     
            Scanner in = new Scanner("datafile.txt");
     
            while(in.hasNext())
            {
                String next = in.next();
                if(optor.isOperator(next))
                    optor.push(next);
                else
                {
                    oprand.push(Integer.parseInt(next));
                }
     
            }
     
            System.out.println("Operators");
            optor.display();
     
            System.out.println("Operands");
            oprand.display();
        }
     
    }
    Last edited by mia_tech; June 25th, 2012 at 05:47 PM.


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: could anyone tell me why am I getting this error?

    Scanner in = new Scanner("datafile.txt") //wrong way

    //RIGHT WAY
    File file = new File("datafile.txt");
    Scanner in = new Scanner(file);

    I hope that helps if the rest of your code is right

  3. The Following User Says Thank You to kindk12 For This Useful Post:

    mia_tech (June 26th, 2012)