• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to ask input from a user?  RSS feed

 
Robin Leckey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I wanted to use "someVariable = SavitchIn.readlineInt();" instead of "Scanner someVariable = new Scanner(System.in)" because that is what my programming teacher uses. Whenever I run the code, there is an error saying:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
SavitchIn cannot be resolved

at GradeClass.main(GradeClass.java:19)

Is there a way to allow a user to enter his/her input without this error? P.S. I am using Eclipse.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin,
Welcome to CodeRanch!

I googled SavitchIn since that is a custom class and found this. If that's the one, you need to copy the class and put in your Eclipse project so Eclipse can find it. (Since it is in the default package, you can't import it any other way)
 
Robin Leckey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Robin,
Welcome to CodeRanch!

I googled SavitchIn since that is a custom class and found this. If that's the one, you need to copy the class and put in your Eclipse project so Eclipse can find it. (Since it is in the default package, you can't import it any other way)


Thanks for the warm welcome Jeanne! But where should I exactly copy and paste the code, in between the "public class" and "public static void main (String[] args)?" I tried copying and paste it in different spots and there are still errors
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you'll need to create a new external class named "SavitchIn", and copy all the code in there. But what advantage does this offer over Scanner?
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write your own utility class to handle keyboard input.Advantages:-
  • 1: You only ever have one Scanner object and never need to close it, nor create a new Scanner for System.in.
  • 2: You don't need to write code to get the input anywhere else.
  • 3: You can overload that method so it will only pass a result in a particular range. Make it throw IllegalArgumentException if max is less than min.
  • 4: You can create nextDouble, nextBigDecimal etc methods too.
  • Disadvantages. Look particularly at No 2:-
  • 1: That code is not thread‑safe. I am leaving it as an exercise to the reader to make that class thread‑safe if required.
  • 2: If an incorrect input is passed, that code will throw an InputMistmatchException. I am leaving it to you to work out how to avoid that Exception completely. Search my posts for "Rob Spoor" and you will find the simple solution which Rob taught me, that allows you completely to avoid that Exception. You can also overload the method to give a general message for incorrect input, or pass a customised message as an argument.
  • 3: There is a risk of scan.nextLine returning an empty String or whitespace only if used after scan.nextAnythingElse. Search my posts and you will find a solution which gets rid of empty Strings. Beware: my first attempt at a solution has a serious mistake in.
  • You now have a utility class which you can use for ever, refining it as you go.

    I suspect the SavtichIn class has not advantages over Scanner, but at least gets you out of having to write new Scanner(...) frequently. The problem such external utilities have is that they do not allow one to see how they are made. The real advantage my class has will only become apparent after you find out how to write getInt so it never throws any Exceptions.
     
    Campbell Ritchie
    Marshal
    Posts: 55785
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:. . . I googled SavitchIn since that is a custom class and found this. . . .
    Oh, that is horrible code. It uses System.in.read, which is a bad method at the best of times. It also uses \n as line end, which requires a special if statement to handle line ends on a Windows® box. In the getInt method it returns -9999 as a sentinel value whi9ch is poor practice since -9999 might be a valid input.

    That does not look like Java® code at all. It looks like C code which has been ported to Java®, bringing its procedural paradigm with it.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Zachary Griggs wrote:It sounds like you'll need to create a new external class named "SavitchIn", and copy all the code in there. But what advantage does this offer over Scanner?


    I just think SavitchIn is more convenient to use than the Scanner method. Do you think you could assist me in creating a new external class?
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I made a public class called, "SavitchIn" and the program is almost working. The thing is that now there's another error:

    The method readLineInt() is undefined for the type SavitchIn

    at SavitchIn.main(SavitchIn.java:17)
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I used JCreator, would my problem be solved? Or should I stick with Eclipse?
     
    Campbell Ritchie
    Marshal
    Posts: 55785
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robin Leckey wrote:If I used JCreator, would my problem be solved? . . .
    No.

    I still think that class you found should be deleted. Why are you using nextLineInt? Either a method returns a line or it returns an int; it won't return both, so you are not going to find a method like nextLineInt in any input program.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Robin Leckey wrote:If I used JCreator, would my problem be solved? . . .
    No.

    I still think that class you found should be deleted. Why are you using nextLineInt? Either a method returns a line or it returns an int; it won't return both, so you are not going to find a method like nextLineInt in any input program.


    I am using nextLineInt(); because you can. I am trying to write code where a user can input five different test scores and then the average can split out. It is a basic task, but Eclipse isn't recognizing the nextLineInt();. It is recognizing the SavitchIn because I made it a class.

    Look at this link: http://cbsd.org/cms/lib010/PA01916442/Centricity/Domain/2023/2%20-%20variables%20and%20user%20input.pdf
    nextLineInt(); is allowed.
     
    Campbell Ritchie
    Marshal
    Posts: 55785
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robin Leckey wrote:. . . nextLineInt(); is allowed.
    No it isn't. It says readLineInt on that pdf.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Robin Leckey wrote:. . . nextLineInt(); is allowed.
    No it isn't. It says readLineInt on that pdf.


    Sorry, my bad. Actually I did type SavitchIn.readLineInt(); in my program before you pointed out my error, but this comes up:

    The method readLineInt() is undefined for the type SavitchIn

    at SavitchIn.main(SavitchIn.java:12)
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16028
    87
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That error message means that the class SavitchIn does not have a method named readLineInt.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Update: I uninstalled Eclipse and I'm using JCreator now. I have this error:
    Error: Could not find or load main class FirstProgram
    And I don't know how to fix it.
     
    Campbell Ritchie
    Marshal
    Posts: 55785
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please tell us which directory the .java and .class files are in, and also what your CLASSPATH is:-
    echo %CLASSPATH%
    on Windows
    or
    echo $CLASSPATH
    on Linux.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Please tell us which directory the .java and .class files are in, and also what your CLASSPATH is:-
    echo %CLASSPATH%
    on Windows
    or
    echo $CLASSPATH
    on Linux.


    I'm sorry, where and how do I find the .java and .class files are in? I'm kind of a beginner programmer. I took an intro to java class my sophomore year of high school and I took another intro to computer programming class this year (Alice, Visual Basic, and C++).
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, another thing that is frustrating is that whenever I download JCreator, the set up wizard does not popup.
     
    Campbell Ritchie
    Marshal
    Posts: 55785
    164
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Use the normal file explorer program to find where the files are.
    You pass the echo XXX instructions to a command line or terminal.
    I haven't used JCreator for such a long time I can't remember anything about setup wizards.
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Robin,

    Somehow we went deeply into technical details rather than discussing what is your chosen way to solve an exercise. Don't you want to share with us what is the project about you're working on?
    Usually such example code snippets quite easily could be found on Oracle tutorials or by looking similar question using our topics search option, but first you need to know what to look for.
    My question is, did you solve your project exercise in your head and documented on a piece of paper?
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not sure if this is right, but wouldn't the .java files and the .class files be in the jdk under my C drive? I really don't know
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Hi Robin,

    Somehow we went deeply into technical details rather than discussing what is your chosen way to solve an exercise. Don't you want to share with us what is the project about you're working on?
    Usually such example code snippets quite easily could be found on Oracle tutorials or by looking similar question using our topics search option, but first you need to know what to look for.
    My question is, did you solve your project exercise in your head and documented on a piece of paper?



    All I want JCreator to do is run a basic HelloWorld program and have SavitchIn work (so a user can input his/her answer to a question or command).
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've watched so many tutorials on YouTube about installing JCreator, and I still can't seem to get it right.
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robin Leckey wrote:I'm not sure if this is right, but wouldn't the .java files and the .class files be in the jdk under my C drive? I really don't know
    No, think about jdk as about a program, which is called java development kit. This program gives you an ability to compile and run your written java source files, which ends *.java. Once you compile your *.java file with java compiler - they become *.class file/-s.

    So, usually your source files would be somewhere else from your jdk instalation directory. They could be anywhere. If you used Eclipse, upon Eclipse install it gives you to choose default location where your all project files will be located, default option if I remember is given as "workspace" in your prefered logical operation system drive: could be C:\ ; D:\
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look up on youtube how to write programs using plain text editor rather than clever programming environments - these could be too difficult at the very beginning.

    Look on youtube things as:
    1. Write Java programs with NotePad++
    2. Compile Java programs from Windows command line
    3. Run Java programs from Windows command line

    Also, please show us what shows you when you type in command line (windows + r; cmd; enter) %PATH%
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:
    Robin Leckey wrote:I'm not sure if this is right, but wouldn't the .java files and the .class files be in the jdk under my C drive? I really don't know
    No, think about jdk as about a program, which is called java development kit. This program gives you an ability to compile and run your written java source files, which ends *.java. Once you compile your *.java file with java compiler - they become *.class file/-s.

    So, usually your source files would be somewhere else from your jdk instalation directory. They could be anywhere. If you used Eclipse, upon Eclipse install it gives you to choose default location where your all project files will be located, default option if I remember is given as "workspace" in your prefered logical operation system drive: could be C:\ ; D:\



    For me, the I extracted the "Java SE Development Kit 8u91 Documentation" to the jdk 1.8.0_91. I did use Eclipse temporarily, but then I decided to uninstall that and download JCreator instead because I am trying to prepare for AP Computer Science A next year (and I'm taking AP CompSci Principles also). I tested out if java was on my computer by typing "javac" in my command prompt and then "javac -version." Should I get rid of that environment variable? I want to refresh the assignments that I completed last year.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I really don't want to use NotePad because the programming classes at my school use JCreator. I know how to program. I'm just so confused on how to set JCreator up. But your instructions are helpful so far. I kind of understand.
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Robin Leckey wrote:I really don't want to use NotePad because the programming classes at my school use JCreator. I know how to program. I'm just so confused on how to set JCreator up. But your instructions are helpful so far. I kind of understand.
    Well, I never used JCreator, nor seen it. Campbell seen it, but unfortunately he can't remember.

    Anyway, lets do it simple. Delete any programming project you created within the JCreator environment and try to create a new fresh one. Create a project with one file which is called Test and copy n' paste the code below and try to execute it.

     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Look up on youtube how to write programs using plain text editor rather than clever programming environments - these could be too difficult at the very beginning.

    Look on youtube things as:
    1. Write Java programs with NotePad++
    2. Compile Java programs from Windows command line
    3. Run Java programs from Windows command line

    Also, please show us what shows you when you type in command line (windows + r; cmd; enter) %PATH%


    Microsoft Windows [Version 10.0.10586]
    (c) 2015 Microsoft Corporation. All rights reserved.


    C:\Users\Robin & Laura>%PATH% //When I type in %PATH%
    'C:\ProgramData\Oracle\Java\javapath' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Users\Robin & Laura>javac //When I type in javac
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are used
    -classpath <path> Specify where to find user class files and annotation processors
    -cp <path> Specify where to find user class files and annotation processors
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -proc:{none,only} Control whether annotation processing and/or compilation is done.
    -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
    -processorpath <path> Specify where to find annotation processors
    -parameters Generate metadata for reflection on method parameters
    -d <directory> Specify where to place generated class files
    -s <directory> Specify where to place generated source files
    -h <directory> Specify where to place generated native header files
    -implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -profile <profile> Check that API used is available in the specified profile
    -version Version information
    -help Print a synopsis of standard options
    -Akey[=value] Options to pass to annotation processors
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system
    -Werror Terminate compilation if warnings occur
    @<filename> Read options and filenames from file


    C:\Users\Robin & Laura>javac -version //When I type in javac -version
    javac 1.8.0_91

     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was mistaken in giving instructions, forgot to type echo %PATH%, sorry.

    But from the other your executed command cna be seen, that all good with PATH variable. Now try to write echo %CLASSPATH% (what Campbell Ritchie suggested you earlier).
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:I was mistaken in giving instructions, forgot to type echo %PATH%, sorry.

    But from the other your executed command cna be seen, that all good with PATH variable. Now try to write echo %CLASSPATH% (what Campbell Ritchie suggested you earlier).


    Microsoft Windows [Version 10.0.10586]
    (c) 2015 Microsoft Corporation. All rights reserved.

    C:\Users\Robin & Laura>echo %PATH%
    C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\C:\Program Files\Java\jdk1.8.0_91/bin;C:\Program Files\Java\jdk1.8.0_91\bin
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:
    Robin Leckey wrote:I really don't want to use NotePad because the programming classes at my school use JCreator. I know how to program. I'm just so confused on how to set JCreator up. But your instructions are helpful so far. I kind of understand.
    Well, I never used JCreator, nor seen it. Campbell seen it, but unfortunately he can't remember.

    Anyway, lets do it simple. Delete any programming project you created within the JCreator environment and try to create a new fresh one. Create a project with one file which is called Test and copy n' paste the code below and try to execute it.




    The same problem is still occuring.
    There are a couple of options on JCreator.
    You can either
    1) Click File ----- New --- File (doesn't have the public class or public static void main(String [] args) ) it only has "public class test" and then "public test"
    2) File --- New --- Project ---- You can choose from Basic Java Application, Basic Java Applet, Empty Project, or Basif JFC Application
    3) File --- New --- Project --- Black Workspace
    Screenshot-(1).png
    [Thumbnail for Screenshot-(1).png]
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Earlier I wrote wrote:try to write echo %CLASSPATH% (what Campbell Ritchie suggested you earlier)
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What JCreator looks like and the third photo in this thread displays the error.
    Screenshot-(2).png
    [Thumbnail for Screenshot-(2).png]
    Screenshot-(3).png
    [Thumbnail for Screenshot-(3).png]
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:
    Earlier I wrote wrote:try to write echo %CLASSPATH% (what Campbell Ritchie suggested you earlier)

    Screenshot-(4).png
    [Thumbnail for Screenshot-(4).png]
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Expand "src" folder, until you see "test.java" file. Then right mouse click on it, and select run/execute or similar.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Expand "src" folder, until you see "test.java" file. Then right mouse click on it, and select run/execute or similar.



    I did exactly what you said and it came up with:

    Error: Could not find or load main class test

    Process completed.

    But, before that popped up, this came up (see photo):
    Screenshot-(5).png
    [Thumbnail for Screenshot-(5).png]
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Clear!

    That is because you created file Test.java (T upper) and inside the file you declared class as test (lower case). Inside the file make it:
    As I said earlier, copy and paste, it seems you decided to rewrite and made a mistake.
     
    Liutauras Vilda
    Marshal
    Posts: 4665
    320
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When the class is declared as public - file name and class declaration inside the file needs to match perfectly (upper case, lower case matters).

    When the class isn't public, then it could differ. But get a habit to name *.java source files and declare class names exactly in the same name.
     
    Robin Leckey
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Clear!

    That is because you created file Test.java (T upper) and inside the file you declared class as test (lower case). Inside the file make it:
    As I said earlier, copy and paste, it seems you decided to rewrite and made a mistake.


    It's still showing up as an error.
    Screenshot-(6).png
    [Thumbnail for Screenshot-(6).png]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!