help with an error please

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

    help with an error please

    I am trying to test a character for being the "Enter" key, I figured to test it
    if(DataBits!=nu ll)
    {
    stuff
    }
    like that, but i got
    H:\>c:\j2sdk1.4 .1\bin\javac.ex e BubbleSort.java
    BubbleSort.java :68: operator != cannot be applied to char,<nulltype>
    if(DataBits!=nu ll)
    ^
    1 error

    if you could please submit a reasonably simple solution I would be very happy, thanks ahead of time for your time.

    Steve
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Shouldn't the enter key produce a newline character? Then you can check

    Code:
    if (DataBits == '\n')
    You can't compare a char with null because char is not a class, but a native type.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by JavaStudent07
      I am trying to test a character for being the "Enter" key, I figured to test it
      if(DataBits!=nu ll)
      {
      stuff
      }
      like that, but i got
      H:\>c:\j2sdk1.4 .1\bin\javac.ex e BubbleSort.java
      BubbleSort.java :68: operator != cannot be applied to char,<nulltype>
      if(DataBits!=nu ll)
      ^
      1 error

      if you could please submit a reasonably simple solution I would be very happy, thanks ahead of time for your time.

      Steve
      Oh dear. What are you trying to do now JavaStudent?

      Just explain what you want to achieve and I'm sure there is a simple way of doing it.

      Comment

      • JavaStudent07
        New Member
        • Jan 2007
        • 64

        #4
        the same program as before lol, three months, probably take you guys one if that XD jeez lol...oh well thanks for the tip :)

        Comment

        • JavaStudent07
          New Member
          • Jan 2007
          • 64

          #5
          Code:
          /*
          Steve Glasser
          January 6,2007
          BubbleSort
          */
          
          import java.io.*;
          import java.util.*;
          
          public class BubbleSort
          {
          public static void main (String args[]) throws Exception
          {
          //while(DataBits == '\n')
          //{
          Sort();
          //}
          }
          //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()throws Exception
          {
          
          int Averages[];
          String filage="";
          char DataBits='A';
          String Number="";
          char array[];
          int PlaceHolder=0;
          int ArrayLength=0;
          int intN=0;
          
          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();
          BufferedReader inData=new BufferedReader(new FileReader(filage));
          String Data=inData.readLine();
          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=(PlaceHolder+1);
          DataBits=(Data.charAt(PlaceHolder));
          }
          }
          
          NumArray[intN]=seekArray(Number);
          System.out.println("Final Concatenated Result of While: "+NumArray[intN]);
          intN++;
          Number="";
          
          if(PlaceHolder<ArrayLength)
          {
          System.out.println("PlaceHolder: "+PlaceHolder);
          System.out.println("Character: *Blank*");
          PlaceHolder++;
          DataBits=(Data.charAt(PlaceHolder));
          }
          }
          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 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;
          
          
          
          for(N=1; N<intN; N++)
          {
          	while(swapped==0)
          	{
                  while(NumArray[N]>NumArray[N-1])
                     {
          	   if(NumArray[N]>NumArray[N + 1]) 
                     {     
          	   swap=NumArray[N];
          	   NumArray[N]=NumArray[N+1];
          	   NumArray[N+1]=swap;
                     }
          	   swapped=0; 
          	   }//end while
                  if(swapped==0)
                  {
          	swapped=1;
                  }
          	}//end while
          }//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 seekPrint(int NumArray[], int intN)
          {
          int intTest=0;
          int intTest1=0;
          
          for(intTest=intN; intTest>=0; intTest--)
          {
          intTest1=intN;
          System.out.println(NumArray[intTest1]);
          intTest1--;
          }
          }
          }
          thats the code

          Comment

          • JavaStudent07
            New Member
            • Jan 2007
            • 64

            #6
            67 34 5 6 23 12
            56 100 97 97 85
            78 67 90
            452 78 345 67 888 78


            ^^^^^
            thats the numbers inputted, at the moment i am workin on the first line only

            Comment

            • JavaStudent07
              New Member
              • Jan 2007
              • 64

              #7
              H:\>c:\j2sdk1.4 .1\bin\java.exe BubbleSort
              Please input the name of the file from which the data will be acquired. Be sure to use the entire pat
              h:\Numbers.txt
              67 34 5 6 23 12
              16
              PlaceHolder: 0
              Character:6
              PlaceHolder: 1
              Character:7
              Final Concatenated Result of While: 67
              PlaceHolder: 2
              Character: *Blank*
              PlaceHolder: 3
              Character:3
              PlaceHolder: 4
              Character:4
              Final Concatenated Result of While: 34
              PlaceHolder: 5
              Character: *Blank*
              PlaceHolder: 6
              Character:5
              Final Concatenated Result of While: 5
              PlaceHolder: 7
              Character: *Blank*
              PlaceHolder: 8
              Character:6
              Final Concatenated Result of While: 6
              PlaceHolder: 9
              Character: *Blank*
              PlaceHolder: 10
              Character:2
              PlaceHolder: 11
              Character:3
              Final Concatenated Result of While: 23
              PlaceHolder: 12
              Character: *Blank*
              PlaceHolder: 13
              Character:1
              PlaceHolder: 14
              Character:2
              Final Concatenated Result of While: 12
              PlaceHolder: 15
              Character: *Blank*
              Exception in thread "main" java.lang.Strin gIndexOutOfBoun dsException: String index out of range: 16
              at java.lang.Strin g.charAt(String .java:460)
              at BubbleSort.Sort (BubbleSort.jav a:72)
              at BubbleSort.main (BubbleSort.jav a:16)

              ^^^^^^^
              thats the print out

              Comment

              • JavaStudent07
                New Member
                • Jan 2007
                • 64

                #8
                input lines of numbers from a separate file and sort them from highest to lowest order

                ^^^
                thats the task,

                PS: there are spaces at the end of each data line otherwise it cuts off the last number, it just worked, i dont know...

                Thanks in advance for ANY HELP lol

                Steve

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JavaStudent07
                  Code:
                  /*
                  Steve Glasser
                  January 6,2007
                  BubbleSort
                  */
                   
                  import java.io.*;
                  import java.util.*;
                   
                  public class BubbleSort
                  {
                  public static void main (String args[]) throws Exception
                  {
                  //while(DataBits == '\n')
                  //{
                  Sort();
                  //}
                  }
                  //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()throws Exception
                  {
                   
                  int Averages[];
                  String filage="";
                  char DataBits='A';
                  String Number="";
                  char array[];
                  int PlaceHolder=0;
                  int ArrayLength=0;
                  int intN=0;
                   
                  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();
                  BufferedReader inData=new BufferedReader(new FileReader(filage));
                  String Data=inData.readLine();
                  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=(PlaceHolder+1);
                  DataBits=(Data.charAt(PlaceHolder));
                  }
                  }
                   
                  NumArray[intN]=seekArray(Number);
                  System.out.println("Final Concatenated Result of While: "+NumArray[intN]);
                  intN++;
                  Number="";
                   
                  if(PlaceHolder<ArrayLength)
                  {
                  System.out.println("PlaceHolder: "+PlaceHolder);
                  System.out.println("Character: *Blank*");
                  PlaceHolder++;
                  DataBits=(Data.charAt(PlaceHolder));
                  }
                  }
                  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 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;
                   
                   
                   
                  for(N=1; N<intN; N++)
                  {
                  	while(swapped==0)
                  	{
                  while(NumArray[N]>NumArray[N-1])
                  {
                  	 if(NumArray[N]>NumArray[N + 1]) 
                  { 
                  	 swap=NumArray[N];
                  	 NumArray[N]=NumArray[N+1];
                  	 NumArray[N+1]=swap;
                  }
                  	 swapped=0; 
                  	 }//end while
                  if(swapped==0)
                  {
                  	swapped=1;
                  }
                  	}//end while
                  }//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 seekPrint(int NumArray[], int intN)
                  {
                  int intTest=0;
                  int intTest1=0;
                   
                  for(intTest=intN; intTest>=0; intTest--)
                  {
                  intTest1=intN;
                  System.out.println(NumArray[intTest1]);
                  intTest1--;
                  }
                  }
                  }
                  thats the code
                  Ok let me just say the following.

                  Suppose you have a text file say
                  Code:
                   String fileName = "test.txt";
                  and having openined it nicely for reading with

                  Code:
                   
                  BufferedReader inData=new BufferedReader(new FileReader(fileName));
                  If you then wish to read one line of data from the file, the first line, then you would do
                  Code:
                   String firstLine = inData.readLine();
                  To read the second line, then you should call readLine again like this
                  Code:
                   String secondLine = inData.readLine();
                  So this is all very well. Just call readLine() to get the next line of data from the file.

                  If you wish to read all the lines of the tex file then you are in need of a while loop that reads until the line line has been read.

                  Now how do we know we have reached the last line? Well readLine() simply returns null if you call it after the last line. So your while loop would look something like this

                  Code:
                   
                  BufferedReader inData=new BufferedReader(new FileReader(fileName));
                  String line = inData.readLine(); 
                  while(line != null) {
                  	 //do stuff with the line
                   
                  	  line = inData.readLine(); 
                  }
                  Having said all that, I now await an explanation from you of where you wanted to know the codes of the enter key being pressed e.t.c ...

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JavaStudent07
                    input lines of numbers from a separate file and sort them from highest to lowest order

                    ^^^
                    thats the task,

                    PS: there are spaces at the end of each data line otherwise it cuts off the last number, it just worked, i dont know...

                    Thanks in advance for ANY HELP lol

                    Steve
                    You mean it just worked as in correctly, no error/exception/incorrect results?

                    Comment

                    • JavaStudent07
                      New Member
                      • Jan 2007
                      • 64

                      #11
                      Originally posted by r035198x
                      You mean it just worked as in correctly, no error/exception/incorrect results?


                      H:\>c:\j2sdk1.4 .1\bin\java.exe BubbleSort
                      Please input the name of the file from which the data will be acquired. Be sure to use the entire path
                      h:\Numbers.txt
                      67 34 5 6 23 12
                      15
                      PlaceHolder: 0
                      Character:6
                      PlaceHolder: 1
                      Character:7
                      Final Concatenated Result of While: 67
                      PlaceHolder: 2
                      Character: *Blank*
                      PlaceHolder: 3
                      Character:3
                      PlaceHolder: 4
                      Character:4
                      Final Concatenated Result of While: 34
                      PlaceHolder: 5
                      Character: *Blank*
                      PlaceHolder: 6
                      Character:5
                      Final Concatenated Result of While: 5
                      PlaceHolder: 7
                      Character: *Blank*
                      PlaceHolder: 8
                      Character:6
                      Final Concatenated Result of While: 6
                      PlaceHolder: 9
                      Character: *Blank*
                      PlaceHolder: 10
                      Character:2
                      PlaceHolder: 11
                      Character:3
                      Final Concatenated Result of While: 23
                      PlaceHolder: 12
                      Character: *Blank*
                      PlaceHolder: 13
                      Character:1
                      PlaceHolder: 14
                      Character:2
                      Exception in thread "main" java.lang.Strin gIndexOutOfBoun dsException: String index out of range: 15
                      at java.lang.Strin g.charAt(String .java:460)
                      at BubbleSort.Sort (BubbleSort.jav a:58)
                      at BubbleSort.main (BubbleSort.jav a:16)
                      ^^^^^^
                      without the spaces after it, it cuts off the whole last number, the !null thing i asked for is at the top of the program where it runs the main "Sort" method, i slashed it out for now but i will try to work with that once i get the base program going.

                      Comment

                      • JavaStudent07
                        New Member
                        • Jan 2007
                        • 64

                        #12
                        its time to go, i will check responses tommorow, good luck understanding a begginers slaughter of a program :-P

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          Maybe you can check if PlaceHolder is greater than the length of the string before getting the new DataBits value - if PlaceHolder IS greater, break from the while loop.

                          Comment

                          • JavaStudent07
                            New Member
                            • Jan 2007
                            • 64

                            #14
                            Originally posted by Ganon11
                            Maybe you can check if PlaceHolder is greater than the length of the string before getting the new DataBits value - if PlaceHolder IS greater, break from the while loop.

                            :) it doesn't work yet, lmao but thanks :) :) it at least doesn't have that gay error w00t

                            Comment

                            Working...