Java: Input strings with keyboard class

前端 未结 5 769
北海茫月
北海茫月 2020-12-20 10:34

I\'m trying to input information in Java console application but I can\'t seem to run it.

This is how my Java file looks like:


public class Ovn         


        
相关标签:
5条回答
  • 2020-12-20 11:01

    I doubt you're still having this problem but import it like this at the top of your code.

    import cs1.Keyboard;
    

    Then it should work properly. For example:

    import cs1.Keyboard;
    public class NamePrinting
    {
        public static void main(String[]args)
        {
            System.out.println ("Enter your name");
            String name = Keyboard.readString();
            System.out.println (name);  
        }
    }
    
    0 讨论(0)
  • 2020-12-20 11:04

    try this

    try{
    
    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter your Number :");
            String  number=buf.readLine();
            System.out.println("your Number is :"+number);
    
    
     }catch(IOException ex){} 
    
    0 讨论(0)
  • 2020-12-20 11:06

    You need to use import java.util.Scanner; for the first line and Use scanner codes instead of the "keyboard" you will get the same result with keyboard. Try like below.

     import java.util.Scanner;
     public class Hello {
    
     public static void main(String[] args) {
        String name;
        System.out.print("enter your name:");
        Scanner input = new Scanner (System.in);
        name = input.nextLine();
    
    
      }
    
    }
    
    0 讨论(0)
  • 2020-12-20 11:08
    Unresolved compilation problem:  
    

    means that the code could not be compiled.

    You have to import Keyboard, something like

    import uitl.Keyboard
    

    When you use classes from a different package (not the same package of the current class), you have to import the class. If the class is also in the same package then you need not import.

    Classes are generally grouped into Packages.
    How do you know the package? Go to the first like of the class. This should be something like package xyz meaning that the current class in the xyz package. The class will be in a folder called xyz then (This is the rule for packages: when you want to have a class in a package, say abc.xyz then the class should have a package declaration - the first line of the code - to be package abc.xyz and the file should be present in a folder xyz which in then should be in a folder abc.

    0 讨论(0)
  • 2020-12-20 11:18
    1. Java is case sensitive. A class file keyboard will contain a class keyboard not Keyboard.
    2. Do you have imported your class? Or are both classes in the same package?
    0 讨论(0)
提交回复
热议问题