Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code?

    My code's not working and I don't know why. I'm trying to read numerical values from a file and saving all instances where a letter is entered instead of a number to a string to be referenced as an error at a later point on in the code. However, there's an error and like I've stated before, I don't know what caused it.
         public static void validateData() throws IOException{
                    File myfile = new File("gradeInput.txt");
                    Scanner inputFile = new Scanner(myfile);
                    for (int i=0; i<33; i++){
                            if(inputFile.hasNextDouble()){
                                    double d = inputFile.nextDouble();
                                    if (d<0||d>99999){
                                            System.out.println("Not a valid ID.");
                                            System.exit(0);
                                    }
                            }
                            else{
                            String s = inputFile.next();
                            }
                            String extraCredit = inputFile.next();
                            String toBeDoubleOne = extraCredit;
                            Double fromStringOne = new Double(toBeDoubleOne);
                            if (fromStringOne<0||fromStringOne>5){
                                    double error1 = fromStringOne;
                            }
                            String grade = inputFile.next();
                            String toBeDoubleTwo = grade;
                            Double fromStringTwo = new Double(toBeDoubleTwo);
                            if (fromStringTwo<0||fromStringTwo>100.00){
                                    double error2 = fromStringTwo;
                            }
     
                    }
                    inputFile.close();
            }
    This is the error that returns.
        Exception in thread "main" java.util.NoSuchElementException
            at java.util.Scanner.throwFor(Scanner.java:838)
            at java.util.Scanner.next(Scanner.java:1347)
            at Paniagua_Grading.validateData(Paniagua_Grading.java:29)
            at Paniagua_Grading.main(Paniagua_Grading.java:6)
    Thanks in advance!
    Last edited by deejaypanininini; March 8th, 2014 at 08:27 PM. Reason: Formatting


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with my code?

    Your formatting left off the [ and ]
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    My code's not working
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    deejaypanininini (March 8th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    Did I do it right?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with my code?

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at Paniagua_Grading.validateData(Paniagua_Grading.jav a:29)
    The program calls the next() method on line 29 when the isn't any more data to read.
    The Scanner class has a method that will test if there is more data so that the program can test for this condition and keep this error from happening.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    Would that be using the while (inputFile.hasNext()) ?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's wrong with my code?

    I don't know about the while() statement, but the hasNext() looks right.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] what s wrong with my code
    By essayd100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2014, 01:59 PM
  2. [SOLVED] can anyone help me what is wrong with my code?
    By marc172 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 09:53 PM
  3. What's wrong with my code?
    By ZorraBella in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 09:37 PM
  4. what is wrong with this code?
    By Linus in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2013, 05:28 PM
  5. What's wrong with my code?
    By shen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 06:34 AM

Tags for this Thread