parseInt?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: parseInt?

  1. #1
    Join Date
    Apr 2005
    Posts
    12

    parseInt?

    Hey guys, so i'm working on an assignment for a class, and i'm having a little difficulty. so the assignment is a Finite State Machine, and what i need it to do is take input from a file via a bufferedReader, and input it into an array based on variable in the file being inputted.

    my problem is, when i'm trying to input the values/variables, when i read them from the BufferedReader, they're coming in as Strings, right? and for the array i need them to be Int values. do i need to parseInt somewhere, and if so, where? i'll post the code i've got so far, but i keep coming up with an error, which i'll also post. Thank you very much to everyone looking and anyone helping. DevX forums have always been a great help and resource.

    Code:
    //Assignment 3
    
    
    import java.io.*;
    import java.util.*;
    
    public class FSA{
    	private String machineName;
    	private int numStates;
    	private String alphabet;
    	private int[][] transitions;
    	private String tape;
    	private int start;
    	private boolean[] acceptStates;
    
         	BufferedReader inp = null;
       	String fileName = null;
       	Scanner keyBD = new Scanner(System.in);
    
        public String FSM(String filename){
    	      try{
    			  System.out.print("File Name: ");
    			  fileName = keyBD.nextLine();
    		  inp = new BufferedReader(new FileReader(fileName));
    
    	String machineName = inp.readLine();
    	int numStates = inp.read();
    	int start = inp.read();
    	String alphabet = inp.readLine();
    
    	int[][] transitions = new int[numStates][alphabet];
    
    	inp.close();
    	}
    	catch(FileNotFoundException e){
    			System.out.println("***File Not Found***");
    		}
    }
    }
    and the compile error i keep getting is:

    Code:
    C:\java\FSA.java:32: incompatible types
    found   : java.lang.String
    required: int
    	int[][] transitions = new int[numStates][alphabet];
                                                                  ^
    Thank you all!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You've declared alphabet to be an object of class String. It cannot be used to dimension the size of your array. Perhaps you want to declare an int alphabetInt which is then given a value when you parse the string value using alphabetInt = Integer.getInteger( alphabet ).

    Of course you could use that value directly in dimensioning your array.

    Have you looked at using the Scanner class, rather than a buffered reader?

  3. #3
    Join Date
    Apr 2005
    Posts
    12
    while i would prefer to use the Scanner class, part of the assignment is specifically using the BufferedReader.

    and i'll try the alphabetInt and parse that. thanks!

  4. #4
    Join Date
    Apr 2005
    Posts
    12
    well, when i change the code around and put that alphabetInt in it gives me a whole bunch of other errors.

    Code:
    C:\java\FSA.java:44: missing return statement
    }
    ^
    C:\java\FSA.java:28: unreported exception java.io.IOException; must be caught or declared to be thrown
    	String machineName = inp.readLine();
                                             ^
    C:\java\FSA.java:29: unreported exception java.io.IOException; must be caught or declared to be thrown
    	line = inp.readLine();
                               ^
    C:\java\FSA.java:31: unreported exception java.io.IOException; must be caught or declared to be thrown
    	lineTwo = inp.readLine();
                                  ^
    C:\java\FSA.java:33: unreported exception java.io.IOException; must be caught or declared to be thrown
    	String alphabet = inp.readLine();
                                          ^
    C:\java\FSA.java:39: unreported exception java.io.IOException; must be caught or declared to be thrown
    	inp.close();
                     ^
    6 errors
    
    Tool completed with exit code 1

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Your FSM method is declared as returning an object of class String, but you are not returning an object.

    The compiler is complaining about the BufferedReader object. Take a look at the manner in which you are defining it. Shouldn't it be in its own little try-catch block?

  6. #6
    Join Date
    May 2005
    Posts
    75
    You're code is kind of confusing but the code below compiles...

    Code:
    //Assignment 3
    
    import java.io.*;
    import java.util.*;
    
    public class FSA {
    
    	private String machineName;
    	private int numStates;
    	private String alphabet;
    	private int[][] transitions;
    	private String tape;
    	private int start;
    	private boolean[] acceptStates;
    
    	BufferedReader inp = null;
    	String fileName = null;
    	Scanner keyBD = new Scanner(System.in);
    
        public String FSM(String filename) throws IOException {
    
    		try{
    
    			System.out.print("File Name: ");
    			fileName = keyBD.nextLine();
    			inp = new BufferedReader(new FileReader(fileName));
    
    			machineName = inp.readLine();
    			numStates = inp.read();
    			start = inp.read();
    			alphabet = inp.readLine();
    
    			int[][] transitions = new int[numStates][Integer.parseInt(alphabet)];
    
    			inp.close();
    
    			return "whatever String you want";
    
    		} catch(FileNotFoundException e){
    
    			return "***File Not Found***";
    
    		}
    
    	}
    
    }
    All I really did was add Integer.parseInt() around your variable "alphabet" in the array declaration. After that the method needed to throw IOException, and it needed return statements since it returns a String. Also, you can remove the variable types inside of the FSM() method since you already cast them as global variables above.

  7. #7
    Join Date
    Apr 2005
    Posts
    12
    Thank you both very very much for the help!

  8. #8
    Join Date
    Mar 2006
    Posts
    4
    hello
    try declaring another variable like this
    int b =Integer.parseInt(numberStates)

    in the array declaration use b instead of numberStates
    Cheerio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center