Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Reading in a file that has both Strings and numbers?

$
0
0
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();
}
 
}
    

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>