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 10 of 10

Thread: Array and counter in for loop

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

    Default Array and counter in for loop

    I need to be able to use the counter and the array in the same loop. The way it is now I get a java.util.NoSuchElementException (no line found). How can i change my code so that it is no longer trying to go past the end of the file.

    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);
            }
     
            //variables
            String[]fractions = new String[100];  //will take in the fractions 
            String[]split = new String[2];        //used to split the fractions
            int [] numerator = new int [100];     // store numerators
            int [] denominator = new int [100];   //store denominators
            int count = 0;                        //number of lines in file
     
            while(inputFile.hasNextLine()){    
               for(int i=0; i<=count ; i++){
                    fractions[i]=inputFile.nextLine();
                    count++;
                    System.out.println(fractions[i]);
               }
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and counter in for loop

    trying to go past the end of the file.
    Call hasNextLine() before every call to nextLine()

    Hint: the terminating condition in a for loop can have multiple sub parts:
      for(int i=0; (i < 10) && (x > 8); i++) {
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Kristenw17 (April 10th, 2013)

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

    Default Re: Array and counter in for loop

    Thank you very much. That fixed it. Don't suppose you can solve my other problem of why I am getting a java.lang.NullPointerException? I tried making my String[]split just a string, but it didn't like that.

    I added on:

    for(int i=0; i<=count; i++){
                split = fractions[i].split("/");
                numerator[i]=Integer.valueOf(split[0]);
                denominator[i]=Integer.valueOf(split[1]);  
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and counter in for loop

    What statement has the variable with the null value? What variable has the null value?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Array and counter in for loop

    The editor highlights

    split = fractions[i].split("/");

    I also tried

    split[i] = fractions [i].split("/");

    but when I add the [i], it tells me that it is an incompatible type because it is an array rather than a string.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and counter in for loop

    Does fractions[i] have the null value? Why does the code assume it has a valid value?
    What is the value of i when the error happens? Does the array have some empty slots in it?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Array and counter in for loop

    Yes. The fractions array has 100 slots. It has that many because I won't know the size of the file that is used to test my code. Is there another way to do the array without setting it at [100]?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and counter in for loop

    You can set the size of the array large enough to hold the data.
    You need to use the number of used slots in the array to control the looping through the array.
    Remember that the max index will be one less than the number of used slots.
    If there is 1 element, the max index is 0.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Array and counter in for loop

    So I have managed to get most of my code done (i hope). I am only having a couple issues. The first is that I am unsure how to distinguish when a fraction has already been counted. For example with a file that looks like:
    6/3
    7/2
    6/3

    The output is:
    6/3 has 2
    7/2 has 1
    6/3 has 2

    Would there be anyway to make the loop(s) skip fractions that have already been counted? Or would I have to created a loop for the system.out.println statements that did that? The code bellow is just the loops to count.

     
            int num;
            int den;
            //compare denominator array
            for(int i = 0; i<=count; i++){
                den = denominator[i];
                num = numerator[i];
                for(int a = 1; a<count; a++){
                    if(den == denominator[a]){ 
                       if(num == numerator[a]){
                            dencount++;
                       }     
                    }
                }
                System.out.println(dencount);
                dencount=0; 
            }
        }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and counter in for loop

    skip fractions that have already been counted
    How can you detect if a fraction has already been counted? Look at the position in the array of the item being counted? Would there be any of the same fractions before it in the array?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. counter in loop not incramenting when its suppose to
    By dendoc01 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 1st, 2013, 09:16 PM
  2. counter not incrementing within for loop - please help!!!
    By javaiscool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2013, 03:46 PM
  3. [SOLVED] Array and for loop
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 5th, 2013, 01:05 PM
  4. Array and loop
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 16th, 2012, 04:04 AM
  5. Help with counter controlled while loop please!
    By rockout341 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 15th, 2011, 02:41 PM