0
public class Test {

    static private Scanner x;

    public static void main(String args[])
    {
        try {
            x=new Scanner(new File("C:\\Users\\scoda\\workspace\\Nikhil\\src\\chinese.txt"));
            x.useDelimiter(" ");
            while(x.hasNext())  
            {

                String a=x.next();
                String b=x.next();              
                String c=x.next();
                System.out.println(a+b+c);

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

My input file is

12 karthik kk
23 gg gg

Expected output:

12karthikkk
23gggg

Actual output:

12karthikkk
23
java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)

I am trying to debug the issue from a long time . Help is appreciated.

5
  • 1
    personally I would read the whole line and then split on " " Commented Jan 16, 2017 at 1:56
  • Have you tried printing a, b, and c inside the loop? Commented Jan 16, 2017 at 2:00
  • I tried reading the whole line i dint work for me Commented Jan 16, 2017 at 2:01
  • I tried printing a,b,c inside loop it prints fine but at the end it shows the exception Commented Jan 16, 2017 at 2:02
  • 1
    Is it possible that the scanner detects that there is something beyond the last "gg", for example a newline? That would mean that String = x.next() would cause the exception. Would you place a counter before the loop and see whether the exception occurs at iteration 4? (start at 1, print before the Scanner#next() calls.) EDIT: basically what shmosel says. Commented Jan 16, 2017 at 2:07

2 Answers 2

4

Because you changed the delimiter to space, the newline isn't counted as a separator, and there are actually only 5 tokens in your string:

  1. 12
  2. karthik
  3. kk

    23

  4. gg
  5. gg

Your code is throwing an exception on the second call to String c=x.next();, because there is no sixth token. If you remove the x.useDelimiter(" "); statement, it will use the default whitespace delimiter, which will split on your newline as well, resulting in 6 tokens.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot , Understood my mistake.
0

Try this

  StringBuffer buffer = new StringBuffer(10);
        try
        {
            x = new Scanner(new File("D:\\test1.txt"));
            x.useDelimiter(" ");

            while (x.hasNext())
            {

                String a = x.next();
                buffer.append(a);

            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(buffer.toString());
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.