Probably very easy question about isNaN() java

  • Java
  • Thread starter BiGyElLoWhAt
  • Start date
  • Tags
    Java
In summary, the conversation is about an individual experiencing an error while using the method isNaN(String) and attempting to tokenize a text document. The conversation also discusses implementing exception handling and possible solutions for converting strings to numbers. The individual is also working on a program in Java to read a file and create a progress report for students, but is encountering errors. They are seeking help and discussing the use of try blocks and skipping tokens.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
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
  • #2
isNaN expects a number, not a string. Did you try converting the string the a number first?
 
  • #3
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:
  • #4
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);

               
            }
           
           
        }
    }
}
 
  • #5
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?
 
  • #6
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
 
  • #7
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.
 

Related to Probably very easy question about isNaN() java

1. What is the purpose of the isNaN() function in Java?

The isNaN() function in Java is used to determine whether a given value is "not a number" (NaN). This is typically used to check if a mathematical expression or variable is a valid number or not.

2. How does the isNaN() function work?

The isNaN() function checks the given value and returns a boolean value of either true or false. If the value is not a number, the function will return true, otherwise it will return false.

3. Can the isNaN() function be used with non-numeric values?

Yes, the isNaN() function can be used with both numeric and non-numeric values. However, it will only return true for non-numeric values, such as strings or boolean values.

4. How is the isNaN() function different from the Number.isNaN() function?

The isNaN() function is a global function in JavaScript, while the Number.isNaN() function is a method specifically for the Number object. The main difference is that the Number.isNaN() function will only return true for values that are of the type "number", while the isNaN() function will attempt to convert the value to a number before checking if it is NaN.

5. Are there any alternatives to using the isNaN() function in Java?

Yes, there are alternative methods to check if a value is NaN in Java. Some options include using the Double.isNaN() or Float.isNaN() methods, as well as using the isFinite() function to check if a value is a finite number.

Similar threads

  • Programming and Computer Science
Replies
3
Views
910
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top