Probably very easy question about isNaN() java

  • Context: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around the use of the isNaN() method in Java, particularly in the context of reading and processing data from a text file. Participants explore issues related to type handling, error management, and data parsing in Java.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports an error when using isNaN() with a Scanner object, questioning the method's applicability to strings.
  • Another participant clarifies that isNaN() expects a number, suggesting that the original poster (OP) should convert the string to a number first.
  • A similar suggestion is made that the OP might be looking for a function to check if a string represents an integer, like Integer.parseInt(), which would require exception handling.
  • The OP shares an updated code snippet, indicating a change in approach to reading input from a file and parsing it into student data.
  • One participant expresses uncertainty about using next() within a try block, questioning whether it would skip tokens and how it affects variable assignment.
  • The OP provides a description of the file format they are working with, detailing the structure of section numbers and student data.
  • The OP reports ongoing issues with filling the scores variable and encountering crashes when creating student objects.

Areas of Agreement / Disagreement

Participants generally agree that isNaN() is not suitable for strings and that conversion to a number is necessary. However, there is no consensus on the best approach to handle the parsing and error management in the OP's code, and the discussion remains unresolved regarding the specific implementation details.

Contextual Notes

Limitations include potential misunderstandings of method functionalities, the need for exception handling in parsing, and the structure of the input data affecting the code's execution.

BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
this is throwing me an error
while(! isNaN(s.next()))
"The method isNaN(String) is undefined for the type ProgressReport"
s is a scanner.

I'm just trying to tokenize a text doc of the form
int
String int ... int
...
String int ... int
int
String int ... int
...
String int ... int
etc.

Oh yea, the question...
Why? What am I doing, or what do I need to do?
 
Technology news on Phys.org
isNaN expects a number, not a string. Did you try converting the string the a number first?
 
mfb said:
isNaN expects a number, not a string. Did you try converting the string the a number first?

I also assume that the OP isn't trying to check for NaN but is actually wants some sort of IsInteger function like Integer.parseInt() but to that one on his data requires exception handling.
 
Last edited:
Yea, I changed how I'm doing it.
Java:
import java.io.*;
import java.util.*;
import java.lang.*;

public class ProgressReport{   
   
    File file = new File("grades.txt");   
    Student[][] student;
    int[] scores;
    String name;
   
   
    public ProgressReport() throws FileNotFoundException{
        readInputFile();
    }
   
   
    private void readInputFile() throws FileNotFoundException{
        Scanner s = new Scanner(file);   
        Scanner line;
        int[] sec = {0,0};
        int k = -1;
        int j = -1;
       
        while(s.hasNextLine())
        {
            String str = s.nextLine();
            k++;
            if(str.length() == 1)
            {
                j++;
                sec[j] = Integer.parseInt(str);
            }
            else{
                line = new Scanner(str);
                while(line.hasNext())
                {
                    try
                    {
                        for(int i =0; ;i++)
                        {   
                            scores[i] = Integer.parseInt(line.next());
                        }
                    }
                    catch(Exception e)
                    {
                        name = line.next();
                    }
                   
                   
                }
                student[j][k] = new Student(name, scores);

               
            }
           
           
        }
    }
}
 
Haven't done java in a while, so I might be doing things stupidly.

If I use next() in a try block, is that going to skip that token? So in other words, will this give me a string number for my name value?
 
The file I'm working with is:
Section number //single digit
Name Score Score Score Score Score //Student data
Student data
...
Section number
student data
student data
 
Hmmm... I'm having some errors. I'm getting the string correctly, but I'm not filling my scores variable, and am crashing everytime I try to create a student.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K