Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default No syntax errors, but run-time error exists?

    //Class:
    public class DigitExtractor {
    //instance
    private String digits;
    private String array;
    public DigitExtractor()
    {
    return;
    }
    public DigitExtractor(String digits)
    {
    this.digits=digits;
    }
    public String extract()
    {
    int diglength=digits.length();
    for(int i = 0;i<(diglength-1);i++)
    {
    array=digits.substring(i);
    }
    return array;
    }
    }


    //Tester

    import java.util.Scanner;
    public class DigitExtractorTester {
    private static Scanner inputstring;

    public static void main(String[] args)
    {
    System.out.println("type in #");
    inputstring=new Scanner(System.in);
    String digits=inputstring.toString();
    DigitExtractor derp=new DigitExtractor(digits);
    System.out.println(derp.extract());

    }
    }


    //Output:

    I expected the scanner to start, but it did not.
    The console displayed:

    type in # //from the System.out.println in the tester program
    //and
    E]


    What does the "E]" mean?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: No syntax errors, but run-time error exists?

    Java Code:
    String digits=inputstring.toString();
    You're getting the toString() representation of a Scanner object, which is not what you want and which is not how you use a Scanner object.
    You may want to read the API entry for this or the tutorial. For my money, I'd use its nextLine() method. Also your extract() method doesn't make much sense to me.

    It's a good lesson though: lack of syntax errors will only get you so far. The program still must make logical sense.

Similar Threads

  1. Syntax Errors
    By andimiami in forum New To Java
    Replies: 8
    Last Post: 09-15-2011, 01:23 PM
  2. ClassNotFoundException error for a class that exists
    By dahaka2011 in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 04-01-2011, 02:44 AM
  3. Eclipse is making me go silly with it's syntax errors
    By magicmojo in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 01:48 PM
  4. Run time errors
    By crabman in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 09:51 PM
  5. Cannot get passed these syntax errors
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •