7 Replies Latest reply: Mar 29, 2009 8:24 PM by 807588 RSS

    'Int cannot be dereferenced' error

    807588
      Greetings,

      I've been wrestling with this all day, trying to get this program to run. The problem seems to be in the final loop but i've tried everything and need a nudge in the right direction.

      Heres the code:
      package Lab;
      import java.util.Scanner;
      
      public class Shifter {
          
          Scanner input = new Scanner(System.in);
          int data[] = new int [ 15 ];
      
          public Shifter ( int size )
          {
          for ( int counter = 0; counter < data.length; counter++ )
               data[ counter ] = 1 + counter; 
          }
          
          public int shift( int pos )
          {
          int count = 0;
          
          for(count = 0; count < data.length; count++)
              data[ count ] = data[ count + pos ];
          return data[ count ];
          }
          
          public void display()
          {
          System.out.printf("Array contents: ");
          int pos = 0;
          
              int value = 0;    
              for(value = 0; value < data.length; value++)
              System.out.printf("%d", data[ value ] );
                            
              System.out.println();
              
              System.out.print("Shift how many positions? ");
              pos = input.nextInt();
                  
                      do{
                          System.out.println();
                          shift( pos );
                              for(value = 0; value < data.length; value++)
                              System.out.printf("%d", data[ value ]); 
                          
                          System.out.print("Shift how many positions? ");
                          pos = input.nextInt();          
                  
                     }while (! pos.matches("[-]?\\d+"));
             
              
          
          }
      }
      The final line in the condition statement is where the problem is, I think. I have to use a do/while loop for this assignment, so it can't be a for loop or a while. I want it to exit when something nonnumeric is entered.
        • 1. Re: 'Int cannot be dereferenced' error
          807588
          You declare pos as an int and you can't call .matches(...) or any methods off of a primitive variable. Also matches is meaningless for ints.
          • 2. Re: 'Int cannot be dereferenced' error
            807588
            I tried to convert pos to a string this way:

                   do{       //loop to continually prompt user until user ends program with non-numeric value
                                System.out.println();
                                shift( pos );
                                    for(value = 0; value < data.length; value++)
                                    System.out.printf("%d", data[ value ]); 
                                
                                System.out.print("Shift how many positions? ");
                                pos = input.nextInt();          
                                String aString = Integer.toString(pos);
                           }while (! pos.equals("[-]?\\d+"));
            and it still gives the same error message. Anyone else know how I might fix this?
            • 3. Re: 'Int cannot be dereferenced' error
              807588
              fearofsoftware wrote:
              I tried to convert pos to a string this way:
              seriously! You create a String "aString" but how can you imagine in any way shape or form that this changes pos?
              • 4. Re: 'Int cannot be dereferenced' error
                807588
                I don't know, that's why I'm asking.
                • 5. Re: 'Int cannot be dereferenced' error
                  807588
                  fearofsoftware wrote:
                  I don't know, that's why I'm asking.
                  pos was born an int, currently is an int, and will always be an int. Make your while condition use this int-ness in its test. If you want to see if it is negative test for it to be < 0. Don't try to ask it to do something it will never be able to do, like act as a String. You need to review some basics.
                  • 6. Re: 'Int cannot be dereferenced' error
                    807588
                    Yes, I do need to review some basics. Thank you for reminding me.

                    Maybe my question wasn't clear. How would I write the condition in the do while loop to say: if the user puts in anything other than an integer value, exit the loop.
                    • 7. Re: 'Int cannot be dereferenced' error
                      807588
                      fearofsoftware wrote:
                      Maybe my question wasn't clear. How would I write the condition in the do while loop to say: if the user puts in anything other than an integer value, exit the loop.
                      If it's not an int, then your parse will throw an exception and your program will end. One thing to do is to make pos a String to begin with, check that it parses well within the loop in a try/catch block and if the parse fails, set a boolean variable that will cause the loop to exit. Or keep pos an int and do the nextInt from within a try/catch block catching whatever exception Scanner throws (the api will tell you) if bad input is passed, then again set the boolean variable that the while loop checks...

                      Edited by: Encephalopathic on Mar 29, 2009 6:23 PM