Reading text file with strings, double and boolean

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • otorojava
    New Member
    • Jan 2008
    • 8

    #1

    Reading text file with strings, double and boolean

    The script works but I'm having a problem reading double and boolean

    Code:
    import java.io.*;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    
    public class FileAddress
    {
      public static void main(String [] args)
      { 
        String aRecord; //for record
        String accountNumber;
        String customerName;
        String customerPhone;
        double customerBalance; //account balance
        boolean accountStatus;  //true if account is active, false if not
    
        StringTokenizer strings;
        
        try
    	{
    		FileReader inStream = new FileReader("account.txt");
            BufferedReader ins = new BufferedReader(inStream);
         
          while ((aRecord=ins.readLine())!= null)
    	  {
            strings = new StringTokenizer(aRecord,"?"); //toekenizer
            if (strings.countTokens() == 5) {
              accountNumber = strings.nextToken();
              customerName = strings.nextToken();
              customerPhone = strings.nextToken();
              customerBalance = strings.nextToken(); //ERROR
              customerStatus = strings.nextToken(); //ERROR
              JOptionPane.showMessageDialog(null,accountNumber); 
              JOptionPane.showMessageDialog(null,customerName+"  "+customerPhone+"  "+customerBalance);
    		  
            }
          }
          ins.close();
        }
    	 catch(IOException e)
    	 {
           e.printStackTrace();
         }
      }
    }
  • otorojava
    New Member
    • Jan 2008
    • 8

    #2
    Ok this is what I did to fix the double variable

    //Double customerBalance ;

    Code:
    customerBalance = strings.nextToken(); //ERROR
    I changed it to

    Code:
    customerBalance  = Double.parseDouble(strings.nextToken());
    Now, how can I resolve string to boolean? I get an error with

    Code:
    boolean(strings.nextToken()); //ERROR

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by otorojava
      Ok this is what I did to fix the double variable

      //Double customerBalance ;

      Code:
      customerBalance = strings.nextToken(); //ERROR
      I changed it to

      Code:
      customerBalance  = Double.parseDouble(strings.nextToken());
      Now, how can I resolve string to boolean? I get an error with

      Code:
      boolean(strings.nextToken()); //ERROR
      In a similar manner ...

      Comment

      Working...