Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: File reading issues

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default File reading issues

    So my end goal is to have read a file containing fractions, and be able to count the number of times a specific fraction appears.
    Example file:
    6/3
    7/2
    6/3
    5/2

    So far I have been able to get the program to count the the number of lines. My idea was to us a for loop to take in a line of the file at a time, and put the numbers from the fraction into an array. My problem is that no matter how many different ways i try this it wont work. It keeps outputting null.

    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.lang.String;
    import java.io.File;
    import java.io.FileInputStream;
     
    public class Assignment1{
     
        public static void main(String [] args){    
            //Reads file containing fractions
            Scanner inputFile = null;
            try {
                inputFile = new Scanner(new FileInputStream("fractions.txt"));
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found or not opened.");
                System.exit(0);
            }
            fractionCounter(inputFile);
     
        }
     
     
        public static void fractionCounter(Scanner file){
           //variables
            String line;                       // place holder for line
            String[]test = new String[100];   // array that holds fractions
            int count= 0;                     // use to count the number of lines
     
     
           //Count the number of lines in the file
           while(file.hasNextLine()){
               count++;
               file.nextLine();
     
               for (int i=0; i<=count; i++){
                   line = file.nextLine();
                   test = line.split("/");
                }
               file.nextLine();
           }
     
           line = file.next();
           System.out.println(line);
     
           //for(int i=0; i<=count; i++){
            //    line = file.nextLine();
            //    test = line.split("/");
     
           //}
     
           //for(int i = 0; i <= count*2; i++){
            //    System.out.println(test[i]);        
           //}
        }     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: File reading issues

    It keeps outputting null.
    Which print statement prints the null? Why do you expect the program to print something other than null?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: File reading issues

    line = file.next();
    System.out.println(line);

    I did the print line to make sure that the fractions were in fact filling the array. They are not. I can't figure out how to put the numbers into the array

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: File reading issues

    It keeps outputting null.
    I don't see what line is printing "null"? The next() method of the Scanner class does not return a null value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: File reading issues

    The error was java.util.NoSuchElementException

    No line was found when using file.next()
    Scanner (Java Platform SE 7 )

    The while(file.hasNextLine()) loop will catch this situation, but I think you need to check the number of times you use next() and nextLine(). Try and simplify it.

    Have you thought about how you are going to store, compare and collate the fractions in the array?

Similar Threads

  1. Reading a file and comparing them to an array to get the file type - java
    By tomb1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 06:13 PM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. [SOLVED] Issues with a test driver file
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2011, 08:54 PM
  4. Issues with writing to text file
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2011, 10:43 AM
  5. file.txt and ReadLine issues
    By chansey123 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 20th, 2010, 04:46 PM

Tags for this Thread