3

I Think I broke this down to the most basic form. If not then I apologize and will try to edit it. Why in my while loop do I need to cast String for the FirstName if I already set the variable to be a String? Am I doing something wrong? I am try to set the FirstName variable to be equal to the first token which is supposed to be the first word of the text file etc.

  try
            {
                BufferedReader infileCust =
                        new BufferedReader(new FileReader("C:\\custDat.txt"));
    }
      catch(FileNotFoundException fnfe)
            {
                System.out.println(fnfe.toString());
            }
            catch(IOException ioe)
            {
                System.out.println(ioe.toString());
            }
        }

        public static void createCustomerList(BufferedReader infileCust,
                CustomerList custList) throws IOException
    {    
        String  FirstName;
        String  LastName;
        int  CustId;


        //take first line of strings before breaking them up to first last and cust ID
        String StringToBreak = infileCust.readLine();
        //split up the string with string tokenizer
        StringTokenizer st = new StringTokenizer(StringToBreak);

        while(st.hasMoreElements()){
        FirstName = (String) st.nextElement();
        LastName = (String) st.nextElement();
        CustId = Integer.parseInt((String) st.nextElement());

        }

Edit: Apprently I can use nextToken() instead. But how come my variables are being shown as not used? Are they not within the scope of the while loop?

Sackling
  • 1,780
  • 5
  • 37
  • 71

3 Answers3

6

This is because nextElement returns Object. If you call nextToken, you would not need to cast.

From the documentation:

public Object nextElement()

Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

EDIT Regarding the variables that are not used: the reason you get the warning is that the variables are assigned, but not printed, saved, or analyzed in some way. If you add a call to, say, writeln with first and last name, the warnings would go away.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you! So nextElement will also use spaces as the delimiter? – Sackling Feb 13 '12 at 00:08
  • @Sackling In your case, it will use whitespace characters `" \t\n\r\f"`. If you pass a different set of delimeters in the constructor, both nextToken() and nextElement() would use these delimeters. – Sergey Kalinichenko Feb 13 '12 at 00:11
3

Because StringTokenizer.nextElement returns a java.lang.Object. See the docs here: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html You can use nextToken() : String instead if you prefer.

snim2
  • 4,004
  • 27
  • 44
2

Because StringTokenizer.nextElement() returns

The same value as the nextToken method, except that its declared return value is Object rather than String.It exists so that this class can implement the Enumeration interface.

RanRag
  • 48,359
  • 38
  • 114
  • 167