1

How do i split a text file ?

say a text file had lines like this :

Jekyll 23.3 33.3 43.2

khaki 32.5 54.3 43.2

.....

so far for my code i have this :

while(scan.hasNext())
     {

          String line = scan.nextLine(); 
          String[] words = .split(" ");

          for(int i = 1; i < words.length - 1; i++)
              System.out.println(words[i]);

edit

now this is printed, my goal is to add all the numbers togeather and then create an average, Im stuck on what needs to be done here

edit entire code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

public class TemperaturesWeek2
{
    public static void main( String [] args ) throws IOException
    {
        int count=0;
        double averageTemp;
        int averageAbove;
        double highestAverage;

        File inputFile = new File (
                         "C:/Users/Phillip/Documents/Temp/temperatures.txt .txt");
        Scanner scan = new Scanner(inputFile);

        while(scan.hasNext()) {

            String words = scan.next();

            double num1 = scan.nextDouble();
            double num2 = scan.nextDouble();
            double num3 = scan.nextDouble();
            double num4 = scan.nextDouble();
            double num5 = scan.nextDouble();
            double num6 = scan.nextDouble();
            double num7 = scan.nextDouble();

            double average = (num1 + num2 + num3+ num4+ num5+ num6+ num7) / 7;

            System.out.println(words);
            System.out.println("average is : " + average);
        } 
    }
}
}
Mitro
  • 1,230
  • 8
  • 32
  • 61
user2985542
  • 61
  • 1
  • 2
  • 9
  • What are you trying to do with the text file? Put the strings into an array? variables? please be more specific – user1050632 Nov 13 '13 at 00:18
  • well once i can seperate the numbers i want to add them togeather and find the average? does that help? – user2985542 Nov 13 '13 at 00:19
  • Not only are you reposting but you create another user account? Wasn't [this](http://stackoverflow.com/questions/19942076/how-to-extract-numbers-from-a-text-file) your original question? – Vivin Paliath Nov 13 '13 at 00:43
  • im sitting next to him we are both in the same programming class stuck on the same project looking for help not answers – user2985542 Nov 13 '13 at 00:48
  • @user2985542 Next time, maybe you could collaborate together to come up with better questions, and show us what you've come up with together? – Tim Post Nov 18 '13 at 06:22

4 Answers4

2

You first need to get the string. And then you need to split/parse it. So if you want to read the file line by line, it would be something like this:

while(scan.hasNext())
{
    String line = scan.nextLine(); // "Read" your file (one line at a time).
    System.out.println(line); // Test what you read.
    String[] words = line.split(" "); // Parse what you read.

    for(int i = 1; i < words.length; i++){
        System.out.println(words[i]);
    }
}
slider
  • 12,810
  • 1
  • 26
  • 42
  • thanks the error is gone but when i run it nothing is outputted? – user2985542 Nov 13 '13 at 00:24
  • Try printing line itself. What do you get? – slider Nov 13 '13 at 00:25
  • it runs and nothing is printed – user2985542 Nov 13 '13 at 00:27
  • what i am trying to do is get the numbers and add them togeather, would this change anything? – user2985542 Nov 13 '13 at 00:28
  • No it doesn't change anything. I changed the code a little. Try running it now and see what it prints out. – slider Nov 13 '13 at 00:31
  • it prints nothing again, when i removed the while loop it just prints the line as it would be normally – user2985542 Nov 13 '13 at 00:34
  • Ok. What are you doing before the while loop? Can you post that code as well? Are you reading the file before the while loop? – slider Nov 13 '13 at 00:40
  • 1
    OP is trying to get his homework done for him. Please don't post the entire answer. – Vivin Paliath Nov 13 '13 at 00:44
  • i've posted my entire code, my goal is to find the number of weeks, find the percentage weeks with an average temp of 90 or higher, and find the week with the highest average, i am trying to do them all spereatly before i add them togeather, i have found the number of weeks and right now i am just needing help on getting the numbers out of the line to add togeather. – user2985542 Nov 13 '13 at 00:45
  • @VivinPaliath im just looking for help not asking you to do it for me – user2985542 Nov 13 '13 at 00:46
  • The help you're asking amounts to "please write the code out for me" and not "please help me understand how to write this code". The latter has already been done for you and you are unlikely to get help with the former. – Vivin Paliath Nov 13 '13 at 00:48
  • i never asked anyone to write the code, i asked why isnt it working and why am i getting this error and why isnt anything being printed. – user2985542 Nov 13 '13 at 00:49
  • You have a semi-colon after scan.hasNext() in your while loop. Try removing that. And check the path to the file. It has 2 .txt in the end. @VivinPaliath I have spare time so I don't mind. I'm only helping him/her debug. – slider Nov 13 '13 at 00:52
  • its a her, and i have removed the semicolon and nothing changed – user2985542 Nov 13 '13 at 00:55
  • What about the path to the file? – slider Nov 13 '13 at 00:56
  • i have been able to print lines from the file so i dont think its the path – user2985542 Nov 13 '13 at 00:58
  • I am running the exact same code on my machine and it prints everything just fine. Why don't you update your question with the new error-free code? – slider Nov 13 '13 at 01:02
  • what is different from your code and the one above? i get this: Welcome to DrJava. Working directory is C:\Users\Phillip\Documents\Temp > run TemperaturesWeek2 > and thats it – user2985542 Nov 13 '13 at 01:04
  • Why do you have a " .txt" in the end after the path? – slider Nov 13 '13 at 01:11
  • i copied the location of the file if i remove the .txt it doesnt even run – user2985542 Nov 13 '13 at 01:11
  • Ok. Why don't you try compiling and running the program from the command prompt? Maybe Dr. Java sucks. I hope you know how to do that. http://stackoverflow.com/questions/12964604/running-java-from-command-line – slider Nov 13 '13 at 01:16
  • it seems that the system.out.println after the for loop doesnt print anything, i put System.out.println(line); after String line = scan.nextLine(); and all the lines from the file where printed but when i moved System.out.println(line); after the for loop it did not print – user2985542 Nov 13 '13 at 01:21
  • can we try adding the numbers togeather and ouputting that sum instead to see if anything changes? – user2985542 Nov 13 '13 at 01:31
  • Sure you can do that. But the code should work (and indeed it is working for me). If it is not working, try printing or use the debugger to inspect what is happening at each stage and you'll know what's wrong. – slider Nov 13 '13 at 01:39
2

Try this

while(scan.hasNext()) {

    String line = scan.nextLine(); 
    String[] words = .split(" ");

    double num1 = Double.parseDouble(words[1]);
    double num2 = Double.parseDouble(words[2]);
    double num3 = Double.parseDouble(words[3]);

    double average = (num1 + num1 + num3) / 3;

    System.out.println(line);
    System.out.println("average is : " + average);
}

Edit: using nextDouble()

while(scan.hasNext()) {

    String words = acan.next();

    double num1 = scan.nextDouble();
    double num2 = scan.nextDouble();
    double num3 = scan.nextDouble();

    double average = (num1 + num1 + num3) / 3;

    System.out.println(word);
    System.out.println("average is : " + average);

    scan.nextLine();
    // add another scan.nextLine() if you have line spaces between your lines
    scan.nextLine() // only if necessary
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

pseudocode

parse each line of the text file.

check to see if the item parsed is an int if int put it into an array if the next character scanned is blank, then increment the array to the next position do until empty file

then a simple for loop to add the contents of the array.

user1050632
  • 680
  • 4
  • 13
  • 26
1

I think this should help

Float.parseFloat(String s)