“Java membaca baris input” Kode Jawaban

Java membaca baris dari file

        Scanner sc = null;
        try {
            File file = new File("myfile.txt"); // java.io.File
            sc = new Scanner(file);     // java.util.Scanner
            String line;
            while (sc.hasNextLine()) {
              line = sc.nextLine();
              // process the line
            }
          }
          catch(FileNotFoundException e)
          {
              e.printStackTrace();
          }
          finally {
            if (sc != null) sc.close();
          }
Zealous Zebra

Java Baca satu baris teks menggunakan pemindai

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    // creates an object of Scanner
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your name: ");
    // takes input from the keyboard
    String name = input.nextLine();

    // prints the name
    System.out.println("My name is " + name);

    // closes the scanner
    input.close();
  }
}
SAMER SAEID

Java membaca baris input

// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
 
class GetInputFromUser {
    public static void main(String args[])
    {
        // Using Scanner for Getting Input from User
        Scanner in = new Scanner(System.in);
 
        String s = in.nextLine();
        System.out.println("You entered string " + s);
 
        int a = in.nextInt();
        System.out.println("You entered integer " + a);
 
        float b = in.nextFloat();
        System.out.println("You entered float " + b);
    }
}
Juliet

Jawaban yang mirip dengan “Java membaca baris input”

Pertanyaan yang mirip dengan “Java membaca baris input”

Lebih banyak jawaban terkait untuk “Java membaca baris input” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya