4 Replies - 280 Views - Last Post: 01 March 2013 - 11:16 AM Rate Topic: -----

#1 chiichannel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 13

Reading in a file that has both Strings and numbers?

Posted 01 March 2013 - 06:29 AM

I need some help reading in a file that has both numbers and strings. I just need help differetiating between Strings and the two numbers at the end of each line.

Example of what I'm reading in:

Meh Farms, 114.7 45674

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.*; 
import java.io.*;
public class Popcorn{ 
  
  
  public static void main (String [] args) throws IOException { 

           
                 System.out.println("Enter the name of the file");
               Scanner in = new Scanner(System.in);
               String filename = in.next(); 
                Scanner infile = new Scanner(new FileReader( filename)); // 
                String line = "" ; 
                
               //to get stuff from the file reader
               
               
                  
                 // int endingIndex =line.indexOf(','); 
               // String fromName = line.substring(0, endingIndex); //this is to get the name of the farm 
               // if (fromName.length()>0){
               //   System.out.println (fromName);
              //  }
               // else if (fromName.length()<= 0)
                //  System.out.println(""); some of the durdling that goes on in my durdle 
               // }
              while (infile.hasNext())
           
                {  
                 line= infile.nextLine().trim();  // added the call to trim to remove whitespace
                   if(line.length() > 0)    // test to verify the line isn't blank
                   { 
                    int endingIndex =line.indexOf(','); 
                  String fromName = line.substring(0, endingIndex);
                  String rest = line.substring(endingIndex + 1);
                
                   //   float numbers = Float.valueOf(rest.trim()).floatValue();
        Scanner inLine = new Scanner(rest);
        
                     while inLine.hasNextDouble():
                   System.out.println(fromName);
                  }
                
  }
              infile.close();
}
 
}
    


Is This A Good Question/Topic? 0
  • +

Replies To: Reading in a file that has both Strings and numbers?

#2 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Re: Reading in a file that has both Strings and numbers?

Posted 01 March 2013 - 06:43 AM

Loop the String and check the Char to see if its a number. After you get the number use String.Replace to remove it from the string. Just because with the whitespace
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7994
  • View blog
  • Posts: 13,679
  • Joined: 19-March 11

Re: Reading in a file that has both Strings and numbers?

Posted 01 March 2013 - 07:46 AM

Meh Farms, 114.7 45674

If this format is consistent the easiest thing to do would be to split (String.split()) on the comma. Now you have two Strings

"Meh Farms" and "114.7 45674"

Take the first as a String. Split the second on " " (space) and use Double.parseDouble() or Integer.parseInt() to convert the two numbers to numeric format.

This post has been edited by jon.kiparsky: 01 March 2013 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,984
  • Joined: 20-September 08

Re: Reading in a file that has both Strings and numbers?

Posted 01 March 2013 - 09:38 AM

What are you meant to do with the data once you've parsed them?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Reading in a file that has both Strings and numbers?

Posted 01 March 2013 - 11:16 AM

you can test the Scanner hasNext(), hasNextInt(), hasNextDouble()

Start by hasNextInt() as if there is a int hasNextDouble() will return true
hasNext() will be true up to th ned of file so test in that order:

hasNextInt() if it is the case read the int and start all over again
hasNextDouble() if it is the case read the double and start all over again
hasNext() if it is the case read the String and start all over again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1