Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Probably very easy question about isNaN() java

Tags:
  1. Jan 10, 2017 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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?
     
  2. jcsd
  3. Jan 10, 2017 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    isNaN expects a number, not a string. Did you try converting the string the a number first?
     
  4. Jan 10, 2017 #3
    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: Jan 10, 2017
  5. Jan 10, 2017 #4

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Yea, I changed how I'm doing it.
    Code (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);

                   
                }
               
               
            }
        }



    }
     
  6. Jan 10, 2017 #5

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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?
     
  7. Jan 10, 2017 #6

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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
     
  8. Jan 10, 2017 #7

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Probably very easy question about isNaN() java
  1. Easy Java Question (Replies: 1)

  2. Question about Java (Replies: 4)

Loading...