This discussion is archived
14 Replies Latest reply: Jan 31, 2009 5:18 PM by 549171 RSS

Problem with reading a string from the stdin

549171 Newbie
Currently Being Moderated
Hello

i would like to take as input a string and i do :
System.out.println("Insert a string\n");

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String read_string = stdin.readLine();
When i compile i have :

Note: C:\Documents and Settings\Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

The program compiles and runs, it's just that i wouldn't like anyone to take that note when compiles it
Thanks
  • 1. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    So do what it says: recompile with the given flag, find out what it's complaining about (probably it has nothing to do with the string input code you've posted) and fix it.
  • 2. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    If you do what it says (compile the file again specifying -Xlint:unchecked as an argument) then the compiler will supply more detail. As the message says.

    If you can't understand the more detailed messages it would be a good idea to post the actual code that is the cause of the problem.
  • 3. Re: Problem with reading a string from the stdin
    549171 Newbie
    Currently Being Moderated
    Well i did it, and the program does not "complain".
    No details appear, why does this happen?
  • 4. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    Not the program itself, the compiler.

    That compiler message is a warning. It tells you that if you recompile with the given argument, it will give you more detailed warning messages. You can then read those warnings, and fix the things that the compiler feels is wrong.

    You don't have to fix them; they're just warnings. Your code will be better though if you do fix them.
  • 5. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    why does this happen?
    Post the Main.java code. Post the actual (and complete) output when javac is run with and without the -Xlint switch.
  • 6. Re: Problem with reading a string from the stdin
    800308 Newbie
    Currently Being Moderated
    Permit me to direct you to my [Noob Tips|http://forums.sun.com/thread.jspa?threadID=5364203] post, especially the bit about posting error messages. I think maybe I need to add a section on compiler warnings, with special reference to the -Xlint option.

    FYI: The command: java -X produces a use message on the Sun Java compilers extended options.
    {noformat}
    Microsoft Windows [Version 6.0.6000]
    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

    C:\Users\Administrator>java -version
    java version "1.6.0_07"
    Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
    Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

    C:\Users\Administrator>java -X
        -Xmixed           mixed mode execution (default)
        -Xint             interpreted mode execution only
        -Xbootclasspath:<directories and zip/jar files separated by ;>
                          set search path for bootstrap classes and resources
        -Xbootclasspath/a:<directories and zip/jar files separated by ;>
                          append to end of bootstrap class path
        -Xbootclasspath/p:<directories and zip/jar files separated by ;>
                          prepend in front of bootstrap class path
        -Xnoclassgc       disable class garbage collection
        -Xincgc           enable incremental garbage collection
        -Xloggc:<file>    log GC status to a file with time stamps
        -Xbatch           disable background compilation
        -Xms<size>        set initial Java heap size
        -Xmx<size>        set maximum Java heap size
        -Xss<size>        set java thread stack size
        -Xprof            output cpu profiling data
        -Xfuture          enable strictest checks, anticipating future default
        -Xrs              reduce use of OS signals by Java/VM (see documentation)
        -Xcheck:jni       perform additional checks for JNI functions
        -Xshare:off       do not attempt to use shared class data
        -Xshare:auto      use shared class data if possible (default)
        -Xshare:on        require using shared class data, otherwise fail.

    The -X options are non-standard and subject to change without notice.

    C:\Users\Administrator>
    {noformat}
    Cheers. Keith.

    Edited by: corlettk on 1/02/2009 10:46 ~~ Clearer.
  • 7. Re: Problem with reading a string from the stdin
    549171 Newbie
    Currently Being Moderated
    Well i have created a LinkedList and i get as output :
    C:\Documents and Settings\Main.java:75: *warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.LinkedList*
    i use boolean add(Object x) and i don't check the return value, i guess that i have to correct that.

    e.g.
    i do:
    LinkedList new_list = new LinkedList();
    String hey = " hello"
    new_list.add(hey)
    Edited by: g_p_java on Jan 31, 2009 4:47 PM
  • 8. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    By the way, if this is new to you... "lint" was originally a program to clean up C programs (unless it has an earlier provenance I'm not aware of). The name refers to lint on clothing -- the program picks at little messy things in the code, the same way that you might use a lint roller to pick off the lint on a jacket. A jacket with lint on it is perfectly wearable, and a program that doesn't pass a lint check without warnings might still be perfectly usable. It's about being obsessive about getting every little thing right, which is a good practice in program development but not an absolute requirement. The name "lint" stuck and is applied to similar checks for other languages, as you see here.

    Just to put this into context.
  • 9. Re: Problem with reading a string from the stdin
    800308 Newbie
    Currently Being Moderated
    paulcw wrote:
    By the way, if this is new to you... "lint" was originally a program to clean up C programs....
    Just to put this into context.
    http://en.wikipedia.org/wiki/Lint_programming_tool
  • 10. Re: Problem with reading a string from the stdin
    549171 Newbie
    Currently Being Moderated
    Thanks, for explaining to me about lint :)
  • 11. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    g_p_java wrote:
    Well i have created a LinkedList and i get as output :
    C:\Documents and Settings\Main.java:75: *warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.LinkedList*
    i use boolean add(Object x) and i don't check the return value, i guess that i have to correct that.

    e.g.
    i do:
    LinkedList new_list = new LinkedList();
    String hey = " hello"
    new_list.add(hey)
    It's not that you're expected to do anything with the return value. Rather it's that you have declared new_list in a nongeneric way.

    Better would be:
    LinkedList<String> newList = new LinkedList<String>();
    String hey = " hello";
    newList.add(hey);
  • 12. Re: Problem with reading a string from the stdin
    843785 Newbie
    Currently Being Moderated
    g_p_java wrote:
    Well i have created a LinkedList and i get as output :
    C:\Documents and Settings\Main.java:75: *warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.LinkedList*
    i use boolean add(Object x) and i don't check the return value, i guess that i have to correct that.
    It wants you to use parameterized types aka Generics, presumably.
  • 13. Re: Problem with reading a string from the stdin
    800308 Newbie
    Currently Being Moderated
    LinkedList new_list = new LinkedList();
    String hey = " hello"
    new_list.add(hey)
    My version (uncompiled and there obviously untested) might look something like:
        List<String> list = new LinkedList<String>();
        String hey = " hello";
        list.add(hey);
    ... which should atleast compile... and the list uses Generics, the proverbial dust buster for those icky sticky lint warnings.

    Cheers. Keith.
  • 14. Re: Problem with reading a string from the stdin
    549171 Newbie
    Currently Being Moderated
    Thanks a lot! :)