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 4 of 4

Thread: Entry level: 2 classes and Exception in thread "main"

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

    Default Entry level: 2 classes and Exception in thread "main"

    Hi All,

    Please kindly help me to identify the problem, I use Eclipse IDE

    public class Emp {
     
    	private String name;
    	private String jobTitle;
     
    	public void setName(String nameIn){
    		name=nameIn;
    			}
     
    	public String getName(){
    		return name;
    	}
     
    	public void setTitle(String TitleIn){
    		jobTitle=TitleIn;
    	}
    	public String getTitle(){
    		return jobTitle;
    	}
     
    	public void cutCheck(double amountPaid){
    		System.out.println("Pay to the order ", name);
    		System.out.println("***$", jobTitle);
    		System.out.println("",amountPaid);
    	}
     
    }

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
     
     
    public class TestClass {
     
    	public static void main(String args[])
    								throws IOException{
    		Scanner diskScanner=new Scanner(new File("emp.txt"));
     
    		for (int empNum=1; empNum<=3;empNum++){
    			payOneEmp(diskScanner);
    		}
    	//	diskScanner.close();
    	}
     
                   static void payOneEmp(Scanner aScanner) {
    		Emp anEmp=new Emp();
     
    		anEmp.setName(aScanner.nextLine());
    		anEmp.setTitle(aScanner.nextLine());
    		anEmp.cutCheck(aScanner.nextDouble());
    		aScanner.nextLine();
     
    	}
     
    }

    <terminated> TestClass
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at TestClass.payOneEmp(TestClass.java:21)
    at TestClass.main(TestClass.java:13)
    Last edited by y0zh; August 31st, 2014 at 01:33 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Entry level: 2 classes and Exception in thread "main"

    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at TestClass.payOneEmp(TestClass.java:21)
    The Scanner class's nextLine() method called on line 21 did not find a line to read.
    Make sure there are enough lines in the file
    or use the Scanner class's hasNextLine() method to test for a line before trying to read it.
    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:

    y0zh (September 1st, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Entry level: 2 classes and Exception in thread "main"

    Quote Originally Posted by Norm View Post
    The Scanner class's nextLine() method called on line 21 did not find a line to read.
    Make sure there are enough lines in the file
    or use the Scanner class's hasNextLine() method to test for a line before trying to read it.
    do you have an idea what kind of structure should be a file to make it readable?
    I tried to fill in the file based on typed in method cutCheck, but got

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at TestClass.payOneEmp(TestClass.java:23)
    at TestClass.main(TestClass.java:13)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Entry level: 2 classes and Exception in thread "main"

    The Scanner class should be able to read most text files.
    I don't know what "typed in method cutCheck" means.
    You should be able to create the file to be read with any text editor.

    Try debugging the code by adding a println() statement after every statement that reads from the file. Have the println() statement print out what was read. That will allow you to see what the program is doing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: August 16th, 2014, 01:34 AM
  2. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  3. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM