Issue with loop in my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JavaStudent07
    New Member
    • Jan 2007
    • 64

    Issue with loop in my code

    I have a feeling it is something simple troubling me, I just cant find out what that problem is. The progam is SO CLOSE! please help me.

    Code:
    public class BubbleSort2
    {
    public static void main (String args[]) throws Exception
    {
    String filage="";
    String linetext;
    
    BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please input the name of the file from which the data will be acquired.  Be sure to use the entire path.");
    filage=inWords.readLine();
    
    while((linetext=inWords.readLine())!=null)
    {
    Sort(filage);
    }
    }
    that little bit is the problem, I can compile and run, but it only executes the first line of the file and asks for the file again, then does the first line AGAIN and asks for the file AGAIN and so on and so forth repeating untill I terminate the box. If you have any suggestions, me and my friend are having the same problem, thanks for your time,

    Steve
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by JavaStudent07
    I have a feeling it is something simple troubling me, I just cant find out what that problem is. The progam is SO CLOSE! please help me.

    Code:
    public class BubbleSort2
    {
    public static void main (String args[]) throws Exception
    {
    String filage="";
    String linetext;
     
    BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please input the name of the file from which the data will be acquired. Be sure to use the entire path.");
    filage=inWords.readLine();
     
    while((linetext=inWords.readLine())!=null)
    {
    Sort(filage);
    }
    }
    that little bit is the problem, I can compile and run, but it only executes the first line of the file and asks for the file again, then does the first line AGAIN and asks for the file AGAIN and so on and so forth repeating untill I terminate the box. If you have any suggestions, me and my friend are having the same problem, thanks for your time,

    Steve
    This is only ossible if you have print out in the sort method which you decided not to post. Please post the whole class.

    Comment

    • JavaStudent07
      New Member
      • Jan 2007
      • 64

      #3
      Originally posted by r035198x
      This is only ossible if you have print out in the sort method which you decided not to post. Please post the whole class.
      Code:
      /*
      Steve Glasser
      January 6,2007
      BubbleSort
      */
      
      import java.io.*;
      import java.util.*;
      
      public class BubbleSort2
      {
      public static void main (String args[]) throws Exception
      {
      String filage="";
      String linetext;
      
      BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please input the name of the file from which the data will be acquired.  Be sure to use the entire path.");
      filage=inWords.readLine();
      
      while((linetext=inWords.readLine())!=null)
      {
      Sort(filage);
      }
      }
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static void Sort(String filage)throws Exception
      {
      char DataBits='A';
      String Number="";
      String Data="";
      char array[];
      int PlaceHolder=0;
      int ArrayLength=0;
      int intN=0;
      
      Data=seekBufferedReaderData(filage);
      
      System.out.println(Data);
      array=Data.toCharArray();
      ArrayLength=array.length;
      System.out.println(ArrayLength);
      DataBits=Data.charAt(PlaceHolder);
      final int ARRAY_SIZE=(ArrayLength-1);
      int NumArray[];
      NumArray=new int[ARRAY_SIZE];
      
      
      while(PlaceHolder<ArrayLength)
      {
      
      while(DataBits!=' ')
      {
      if(PlaceHolder<ArrayLength)
      {
      System.out.println("PlaceHolder: "+PlaceHolder);
      
      System.out.println("Character:"+DataBits);
      Number=(Number+DataBits);
      PlaceHolder++;
      DataBits=(Data.charAt(PlaceHolder));
      }
      }
      
      NumArray[intN]=seekArray(Number);
      System.out.println("Final Concatenated Result of While: "+NumArray[intN]);
      intN++;
      Number="";
      
      System.out.println("PlaceHolder: "+PlaceHolder);
      System.out.println("Character: *Blank*");
      PlaceHolder++;
      
      if(PlaceHolder<ArrayLength)
      {
      DataBits=(Data.charAt(PlaceHolder));
      }
      }
      seekAverage(NumArray, intN);
      System.out.println("In sorted order, the numbers go:");
      seekOrder(NumArray, intN);
      }
      
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static String seekBufferedReaderData(String filage)throws Exception
      {
      
      BufferedReader inData=new BufferedReader(new FileReader(filage));
      String Data=inData.readLine();
      
      
      return(Data);
      }
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static int seekArray(String Number)
      {
      int Numerical=Integer.parseInt(Number);
      
      return(Numerical);
      }
      
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static void seekOrder(int NumArray[], int intN)
      {
      int swapped=0;
      int swap=0;
      int N=0;
      
      
      while(swapped==0)
      {
      swapped=1;
      
      for(N=0; N<intN-1;N++)
      {
      	   if(NumArray[N]>NumArray[N + 1]) 
                 {     
      	   swap=NumArray[N];
      	   NumArray[N]=NumArray[N+1];
      	   NumArray[N+1]=swap;
      	   swapped=0; 
                 }
      }
      }//end for
      seekPrint(NumArray, intN);
      }//end Order
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static void seekAverage(int NumArray[], int intN)
      {
      double dblAverage=0.00;
      int intTest=0;
      
      for(intTest=intN-1; intTest>=0; intTest--)
      {
      dblAverage=dblAverage+NumArray[intTest];
      }
      dblAverage=(dblAverage/intN);
      System.out.println("The Average of the Numbers is: "+dblAverage);
      }
      //V^V^V^V^V^V^V^V^V^V^V^V^^V^V^V^V^V^V^V^V^V^V^V^V^V^V^V^
      public static void seekPrint(int NumArray[], int intN)
      {
      int intTest=0;
      
      for(intTest=intN-1; intTest>=0; intTest--)
      {
      
      System.out.println("Array #: (test for subscript)"+intTest+"  "+NumArray[intTest]);
      }
      }
      }

      Comment

      • JavaStudent07
        New Member
        • Jan 2007
        • 64

        #4
        I was wondering if you coult do the "String Data=inData.rea dLine(Counter); " to read each line and just bump it up but it said it couldn't resolve the symbol, I pretty much am out of guesses at this point.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JavaStudent07
          I was wondering if you coult do the "String Data=inData.rea dLine(Counter); " to read each line and just bump it up but it said it couldn't resolve the symbol, I pretty much am out of guesses at this point.
          What do you want the lines

          Code:
           
          String filage="";
          String linetext;
          
          BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Please input the name of the file from which the data will be acquired.  Be sure to use the entire path.");
          filage=inWords.readLine();
          
          while((linetext=inWords.readLine())!=null)
          {
          Sort(filage);
          }
          to do?

          Comment

          • JavaStudent07
            New Member
            • Jan 2007
            • 64

            #6
            I want them to find the data file inputted at the command prompt and run the program for each line with the while statement

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JavaStudent07
              I want them to find the data file inputted at the command prompt and run the program for each line with the while statement
              Then
              Code:
              Sort(filage);
              should be
              Code:
               Sort(linetext);

              Comment

              • JavaStudent07
                New Member
                • Jan 2007
                • 64

                #8
                Code:
                public static void main (String args[]) throws Exception
                {
                String filage="";
                
                BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Please input the name of the file from which the data will be acquired.  Be sure to use the entire path.");
                
                
                while((filage=inWords.readLine())!=null)
                {
                Sort(filage);
                }
                
                }
                ^^^^
                that gives the same repeating results

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JavaStudent07
                  Code:
                  public static void main (String args[]) throws Exception
                  {
                  String filage="";
                   
                  BufferedReader inWords=new BufferedReader(new InputStreamReader(System.in));
                  System.out.println("Please input the name of the file from which the data will be acquired. Be sure to use the entire path.");
                   
                   
                  while((filage=inWords.readLine())!=null)
                  {
                  Sort(filage);
                  }
                   
                  }
                  ^^^^
                  that gives the same repeating results
                  So filage is actually a line of data from your file.
                  Now have a look at
                  Code:
                   Data=seekBufferedReaderData(filage);
                  .

                  And that method takes the fileage and tries to open a file called filage!

                  Comment

                  • JavaStudent07
                    New Member
                    • Jan 2007
                    • 64

                    #10
                    sorry, I dont follow. I just want to fill an array with each subscript being a different line of the test values

                    EX.
                    1.) 2 3 4 5
                    2.) 3 6 2 6
                    3.) 9 2 5 6

                    i want NumberArray[0]==2 3 4 5 and NumberArray[1]==3 6 2 6, etc etc. I think I can work it out if I can get some direction on that.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by JavaStudent07
                      sorry, I dont follow. I just want to fill an array with each subscript being a different line of the test values

                      EX.
                      1.) 2 3 4 5
                      2.) 3 6 2 6
                      3.) 9 2 5 6

                      i want NumberArray[0]==2 3 4 5 and NumberArray[1]==3 6 2 6, etc etc. I think I can work it out if I can get some direction on that.
                      Code:
                       index = 0; 
                      for each line in the file
                      String[] two = line.split(")"); // or is it line.split("\\)")?
                      numberArray[index] = two[1];

                      Comment

                      • JavaStudent07
                        New Member
                        • Jan 2007
                        • 64

                        #12
                        i'm sorry, could you integrate it into my code or some other code for an example of actual coding, I'm not sure based on what you said, thats all new stuff to me...i'm sorry for the inconvenience

                        Steve

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by r035198x
                          Code:
                           index = 0; 
                          for each line in the file
                          String[] two = line.split(")"); // or is it line.split("\\)")?
                          numberArray[index] = two[1];
                          Do you understand what the above is trying to do?

                          Comment

                          • JavaStudent07
                            New Member
                            • Jan 2007
                            • 64

                            #14
                            no i dont sorry

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by JavaStudent07
                              sorry, I dont follow. I just want to fill an array with each subscript being a different line of the test values

                              EX.
                              1.) 2 3 4 5
                              2.) 3 6 2 6
                              3.) 9 2 5 6

                              i want NumberArray[0]==2 3 4 5 and NumberArray[1]==3 6 2 6, etc etc. I think I can work it out if I can get some direction on that.
                              Is the data in your file numbered like the example above or is the data just like this

                              2 3 4 5
                              3 6 2 6
                              9 2 5 6

                              Comment

                              Working...