This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Mar 9, 2008 1:29 PM by 800282 RSS

int cannot be dereferenced

807591 Newbie
Currently Being Moderated
Hi! I keep getting this "int cannot be dereferenced" error msg all the time... Can you help me correct my codes please??

import java.io.*;
import java.util.*;

public class Q4
{
     public static void main(String [] args)
     {
          try
          {
               
          Scanner sf=new Scanner(new FileReader("Input.txt"));

int x=1;
while (x.hasNext())
{
x=sf.nextInt();
System.out.print(x + " ");
}
}
catch (IOException e)
{
     System.out.println("Input error");
}

     }
}

Thanks a lot...
  • 1. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    Your int var doesn't have a "haveNext( )" method. Your Scanner variable however does. Use this instead.
  • 2. Re: int cannot be dereferenced
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Just what are you trying to achieve with this line?
    while (x.hasNext())
    Hint: Which object do you really want to ask whether it has a next?

    To post code, use the code tags -- [code]Your Code[/code]will display as
    Your Code
    Or use the code button above the editing area and paste your code between the tags it generates.

    And when you make a mistake like this, the compiler tells you everything you need to know to locate the error.

    db
  • 3. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    I am trying to write a java program which uses Scanner class to read an unknown number of integers from a file into an array until there are no values left. And the teacher recommended to use scanner method hasNext()...
  • 4. Re: int cannot be dereferenced
    800282 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    ... And the teacher recommended to use scanner method hasNext()...
    But you are not doing that. Carefully read reply #1 again, and you'll spot the error.
  • 5. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    I am trying to write a java program which uses Scanner class to read an unknown number of integers from a file into an array until there are no values left. And the teacher recommended to use scanner method hasNext()...
    Please read my post again. You're calling int#hasNext() instead of Scanner#hasNext(). You're calling it on the wrong variable!
  • 6. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    Im sorry, I ve been using java for only a few months and I think I dont understand many of the fundamental things yet...Can you plz correct the code for me so I can see my mistake?? Because I dont know how to do that now... :(
  • 7. Re: int cannot be dereferenced
    800282 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    Im sorry, I ve been using java for only a few months and I think I dont understand many of the fundamental things yet...Can you plz correct the code for me so I can see my mistake?? Because I dont know how to do that now... :(
    Comon, this is not rocket science.
    Scanner sf=new Scanner(new FileReader("Input.txt"));
    
    int x=1;
    while (x.hasNext()) // you're saying here: "while the number 1 has a next token ..." 
    Shouldn't that be something else? Shouldn't you poll that Scanner sf thing for the availability of more tokens?
  • 8. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    Yess, exactly...But I dont know how to change it??

    I especially dont understand how to use this " .hasNext "... I havent seen any examples of it yet... Can you please change my code so that I can have 1 correct example??
  • 9. Re: int cannot be dereferenced
    DarrylBurke Guru Moderator
    Currently Being Moderated
    I'm willing to bet that yagmur_09 didn't personally write even a single line of the code he/she/it posted.

    db
  • 10. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    I wrote them all bymyself sir...
  • 11. Re: int cannot be dereferenced
    800282 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    Yess, exactly...But I dont know how to change it??
    Looking at the time differences of your posts, it doesn't look like you're trying very hard. Neither are you really trying to understand that what has been posted in your thread.
    \\
    \\
    I especially dont understand how to use this " .hasNext "...
    .hasNext() is a method from the Scanner class. It returns either true or false. And that *while(X) { ... }* statement keeps looping until X is true. The name says it all: .hasNext() returns true if there are still tokens inside the Scanner, or false when it's at the end.
    \\
    \\
    I havent seen any examples of it yet...
    Google has examples.
    \\
    \\
    Can you please change my code so that I can have 1 correct example??
    No, you should really figure this out by yourself. If you can't do that, you should start at chapter 1 of your text book.
  • 12. Re: int cannot be dereferenced
    800282 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    I wrote them all bymyself sir...
    Then please explain what you are trying to do with:
    int x = 1;
    while(x.hasNext())
  • 13. Re: int cannot be dereferenced
    807591 Newbie
    Currently Being Moderated
    Thanks for the help... Really appreciated....
  • 14. Re: int cannot be dereferenced
    800282 Newbie
    Currently Being Moderated
    yagmur_09 wrote:
    Thanks for the help... Really appreciated....
    Did you find the error, or was that just sarcasm?
1 2 Previous Next