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: FileScan and Scanner issue! HELP

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Solved]FileScan and Scanner issue! HELP

    So, I keep getting a java.util.InputMismatchException null(in java.util.Scanner). I have used the same logic in a different program with a list of integers. This program takes from a list of numbers from 1-2000. I've made sure that "integers.txt" is an actual text file in the project. I am using BlueJ to code this. Can anyone shed some light on this?
    import java.util.Scanner;
    import java.io.*;
     
    public class Sorting
    {
        private static int[] a = new int[2000];
        private int v;
        private Scanner fileScan, dataScan;
        private static String oneLine;
        public Sorting() throws IOException
        {
            int place = 0;
            fileScan = new Scanner(new File("integers.txt"));
            while(fileScan.hasNext())
            {    
                oneLine = fileScan.nextLine();
                dataScan = new Scanner(oneLine);
                v = dataScan.nextInt(); //it errors here
     
                a[place] = v;
                place++;
            }
        }
     
        public static void main(String args[]) throws IOException
        {
            Sorting s = new Sorting();
            BubbleSort bubble = new BubbleSort();
            InsertionSort insert = new InsertionSort();
            SelectionSort select = new SelectionSort();
     
            bubble.sort(a);
            insert.sort(a);
            select.sort(a);
     
            System.out.println("Bubble Sort had " + bubble.getCompare() 
                + " comparisons and " + bubble.getExchange() + " exchanges.");
     
            System.out.println("Insertion Sort had " + insert.getCompare() 
                +" comparisons and " + insert.getExchange() + " exchanges.");
     
            System.out.println("Selection Sort had " + select.getCompare() 
                +" comparisons and " + select.getExchange() + " exchanges.");
        }
    }
    Last edited by mm4474; September 23rd, 2012 at 08:01 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,650
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: FileScan and Scanner issue! HELP

    Add a println() to the code to print out the value of the line read from the file so you can see what the Scanner method is trying to read with nextInt().
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileScan and Scanner issue! HELP

    I did a println(oneLine) and received this: "{\rtf1\ansi\ansicpg1252\cocoartf1187"

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileScan and Scanner issue! HELP

    I fixed it. The txt file was messed up. Thanks for your help.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,650
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: FileScan and Scanner issue! HELP

    printlns are useful for seeing what the computer is seeing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: FileScan and Scanner issue! HELP

    Quote Originally Posted by mm4474 View Post
    I did a println(oneLine) and received this: "{\rtf1\ansi\ansicpg1252\cocoartf1187"
    The text file is not "messed up", but rather it's not a text file at all but instead an rtf or "rich text format" file, the default file type created by Window's WordPad program. You need to save it as text for it to be text.

Similar Threads

  1. scanner and int issue
    By saintnicks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 24th, 2012, 01:01 AM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. Bizarre Issue with Scanner Failing to "notice" rest of file
    By nitrogenFingers in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: November 14th, 2011, 10:56 PM
  4. Scanner help
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 11:55 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM