-2

Im trying to read a line of file that has words and numbers in it and save it into a array

The file reads:

Barrett Edan 70 45 59

Part of the code that has a problem:

** let it be known that field was declared as a string array earlier in the code**
String [] fields = new String[100];
int [] numfields = new int[100];

while (reader.hasNext()) {     //while reader reads a line
  line = reader.nextLine();  //line reads reach line of file
  fields = line.split("\t");  //splits line by tab space and saves it into fields
  
  last[count] = fields[0];   // saves first word to first element of array
  first[count] = fields[1]; //saves second word to second element
  numfields[0] = fields[2]; /* THE PROBLEM: now i expected the 3rd word (70) of fields to be able to save into numfields array , but thats when i get cannot be converted to int error
  numfields[1] = fields[3];
  numfields[2] = fields[4];    */
Jah
  • 9
  • 2

3 Answers3

0

You have to explicitly convert from String to int. Java will not do this for you automatically.

numfields[0] = Integer.parseInt(fields[2]);
// and so on...

Presumably this line of data pertains to a single "thing" in whatever problem you're working on. Parallel arrays area bad habit to get into. Rather you want one array/list/whatever composed of objects which hold all of the data pertaining to a single "thing."

Chris
  • 26,361
  • 5
  • 21
  • 42
  • I appreciate the feedback! I'm new to java/coding in general so I'll definitely look into ways to avoid parallel arrays – Jah Apr 10 '23 at 02:40
  • You can say thank you on SO by marking as "accepted" whichever answer you feel has best answered your question. And glad I could help. – Chris Apr 10 '23 at 02:40
  • @Chris There are nearly two million questions tagged [java] on SO. Any basic question on Java, including this one, has almost certainly been asked and answered already. Check for duplicates before answering any basic question. – skomisa Apr 10 '23 at 21:34
0

Personally, I might be tempted to make use of another Scanner to parse the line, for example...

String line = "Barrett Edan 70 45 59";
Scanner scanner = new Scanner(line);
String firstName = scanner.next();
String lastName = scanner.next();
while (scanner.hasNextInt()) {
    System.out.println(scanner.nextInt());
}

Now, you "could" make use of the reader to do this directly, but I'd find it easier to use a seperate Scanner, but that's me

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You should first check that array element is integer or not then convert element from string to int using Integer.parseInt(String s) method.

One example of your code:

if (isInteger(fields[2])) {
    numfields[0] = Integer.parseInt(fields[2]);
}

helper method

public static boolean isInteger(String s) {
    try {
        Integer.parseInt(s);
    } catch (NumberFormatException nfe) {
        // if provided string is not an integer
      return false;
    }
     // if provided string is an integer
     return true;
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • The helper method is not necessarily a great way to do things. If the value can be parsed as an int, you'll have to do the work of parsing the value _twice_. Better to just call `Integer.parseInt` once and handle the exception. – Chris Apr 10 '23 at 04:16