Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read File, SetSelectIndex, Primative Type Error

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I am reading data from a file and want to populate several comboBoxes and JText fields. At first I tried reading the data into arrayLists and than populating the fields. I keep on getting "a primative type int error."

Could someone please look at my code and help me identify my error. The class is called from an menu using the Open File option. The agmodTest class holds the comboBox and JText components. Thanks...Ronnie


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JFileChooser;

public class open implements ActionListener{

File fileName;
String line, mnth;
int i = 0;

public void actionPerformed(ActionEvent ae){

// create new JFileChooser object and set the location to the current directory.
JFileChooser fc = new JFileChooser(".");
File file = null;
fc.setDialogTitle("Browse for text file");
fc.setApproveButtonToolTipText("Open File");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

//get the input of the user (open, or cancel) store the value as an int
int selected = fc.showOpenDialog(null);

//based on the input of the user use a switch statement to execute the correct actions
switch (selected) {
case JFileChooser.APPROVE_OPTION:
file = fc.getSelectedFile();
//set the file just retrieved to the file variable declared at the top of the class.
this.fileName = file;
try{
BufferedReader finhandle = new BufferedReader(new FileReader(fileName));

while((line = finhandle.readLine()) != null) {
// gets comma delimited data
String temp ="," ;
//gets the index of the first character of the string.
int ind = line.indexOf(temp);
String pidAndGrade = line.substring(0, line.length()); // gets the pid and the following submissions

//creates a new string for the pid and grade
StringTokenizer st = new StringTokenizer(pidAndGrade, ",", false);

i = 0;
while (st.hasMoreTokens()){
agmodTest.cupName = (st.nextToken().trim());
agmodTest.countyIndex.setSelectedIndex(Integer.parseInt(st.nextToken().trim());
agmodTest.aWUCAIndex.setIndex(Integer.valueOf((String) st.nextToken().trim()).intValue());
agmodTest.aCropIndex.add(i, st.nextToken().trim());
i++;
}
} //end while loop
}//end try
catch(Exception e){
System.out.println ("Error with open file class..." + e);
e.printStackTrace();
}
}
 
What kind of error is that?
Is that a compile error?

If so, please post the exact message you're getting.

Cheers,

Dian
 
The error is: cannot invoke setSelectIndex() on the primative type int. - Thanks
 
What is agmodTest.countyIndex?

I hope it's not an int ...

I also hope you mean setSelectedIndex with setSelectIndex

Cheers,

Dian
 
Hi Dian - Thank you for looking at my code. The code does say setSelectedIndex. The agmodTest.countyIndex is a combobox index that I am trying to set from data that I am reading from a text (comma delimted) file. I think its an int. I am a little confused as to the difference between an int and and integer. Again, thanks for your help...Ronnie
 
Hi Dian - Thanks for looking at my code and getting back to me. agmodTest.countryIndex is an index for a comboBox and should be an integer. I am some what not clear the difference between int and integer. Also, don't know when to use what and how/if I can convert between them. The code does have setSelectedIndex. My question had the typo. Thanks again...Ronnie
 
Integer is a Class, while int is a primitive type.

You may not call a method on a primitive typ, like
String s = 7.toString ();
You may create a Integer from an int this way:

Integer seven = new Integer (7);

and reverse:

int sixPlusOne = seven.intValue ();

Everything which accepts an Object will not accept an primitive type like int.
Perhaps you try to put an int into a List, which will only accept Objects.

seeking a job as java-programmer in Berlin:
 
int or Integer, none of them have a setSelectedIndex method.

If countryIndex is an int you should do

Code:
agmodTest.countyIndex = (Integer.parseInt(st.nextToken().trim());

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top