Example usage for java.util Scanner Scanner

List of usage examples for java.util Scanner Scanner

Introduction

In this page you can find the example usage for java.util Scanner Scanner.

Prototype

public Scanner(ReadableByteChannel source) 

Source Link

Document

Constructs a new Scanner that produces values scanned from the specified channel.

Usage

From source file:Main.java

public static void main(String[] args) {
    char[][] original = new char[3][3];
    Scanner s = new Scanner(System.in);
    System.out.println("Enter values: ");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            original[i][j] = s.next().charAt(0);
        }/* www .  j  a v  a 2 s  .com*/
    }
    rotate(original);
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {

    int i;//from   w w  w  .j  a v  a2s .c om
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("Testing Scanner 10 12.2 one true two false");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    while (src.hasNext()) {
        if (src.hasNextInt()) {
            i = src.nextInt();
            System.out.println("int: " + i);
        } else if (src.hasNextDouble()) {
            d = src.nextDouble();
            System.out.println("double: " + d);
        } else if (src.hasNextBoolean()) {
            b = src.nextBoolean();
            System.out.println("boolean: " + b);
        } else {
            str = src.next();
            System.out.println("String: " + str);
        }
    }

    fin.close();
}

From source file:Lab2.java

/**
 * @param args the command line arguments
 *//*from   w w w  .j av  a 2s .  c  om*/
public static void main(String[] args) {
    Preprocessor p = new Preprocessor();
    p.parse();
    Query q;
    boolean cont = true;
    Scanner input = new Scanner(System.in);
    String yesOrNo;
    String oOrC;
    ArrayList<Document> scores;

    System.out.println("Would you like to use cosine similarity or okapi to score queries? (C/O)");
    oOrC = input.nextLine();
    while (cont) {
        System.out.println("Enter a query: ");
        /**
         * We don't need the query class, we can just stem it and stopword it then make a document out of it.
         */
        q = new Query(input.nextLine());

        if (oOrC.equals("O") || oOrC.equals("o")) {
            scores = p.calcQueryOkapiScore(q);
        } else {
            scores = p.calcQueryCosineSimilarity(q);
        }

        System.out.println("Documents returned from cosine similarity analysis on query are: ");
        for (Document doc : scores) {
            System.out.println("Text :" + doc.origUtterance);
        }

        System.out.println("Would you like to enter another? Y/N");
        yesOrNo = input.nextLine();
        if (yesOrNo.equals("N") || yesOrNo.equals("n")) {
            cont = false;
        }
    }
}

From source file:it.pcan.test.integration.amqp.MainServer.java

/**
 * @param args the command line arguments
 * @throws java.lang.Exception/*from   w ww .  j av  a2 s .co  m*/
 */
public static void main(String[] args) throws Exception {
    try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:/config/serverApplicationContext.xml")) {
        new Scanner(System.in).nextLine(); // Press Enter key to stop
        System.out.println("Done.");
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {

    AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
    InetSocketAddress address = new InetSocketAddress("localhost", 5000);

    Future<Void> future = client.connect(address);
    System.out.println("Client: Waiting for the connection to complete");
    future.get();/* w  w w  .  j  ava  2s.c  o  m*/

    String message = "";
    while (!message.equals("quit")) {
        System.out.print("Enter a message: ");
        Scanner scanner = new Scanner(System.in);
        message = scanner.nextLine();
        System.out.println("Client: Sending ...");
        ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
        System.out.println("Client: Message sent: " + new String(buffer.array()));
        client.write(buffer);
    }
}

From source file:comparetopics.CompareTwoGroupTopics.java

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("please input the path for File1: ");
    String filepath1 = sc.nextLine();
    System.out.println("please input the path for File2: ");
    String filepath2 = sc.nextLine();

    try {/* ww w  .  j  av  a2 s .  c  o  m*/
        File file1 = new File(filepath1);
        File file2 = new File(filepath2);
        System.out.println("File1: " + filepath1);
        System.out.println("File2: " + filepath2);

        if (!file1.exists()) {
            System.out.println("File1 isn't exist");
        } else if (!file2.exists()) {
            System.out.println("File2 isn't exist");
        } else {
            try (InputStream in1 = new FileInputStream(file1.getPath());
                    BufferedReader reader1 = new BufferedReader(new InputStreamReader(in1))) {

                String line1 = null;
                int lineNr1 = -1;
                while ((line1 = reader1.readLine()) != null) {
                    ++lineNr1;

                    int lineNr2 = -1;
                    String line2 = null;
                    try (InputStream in2 = new FileInputStream(file2.getPath());
                            BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2))) {
                        while ((line2 = reader2.readLine()) != null) {
                            ++lineNr2;
                            compareTwoGroups(line1, line2, lineNr1, lineNr2);
                        }
                    }
                    System.out.println();
                }
            }
        }

    } catch (IOException ex) {
        Logger.getLogger(CompareTopics.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:irckafka.ChatApp.java

public static void main(String[] args) {

    groupId = RandomStringUtils.random(8, true, true);

    boolean isOnline = true;

    Scanner in = new Scanner(System.in);
    while (isOnline) {

        String[] input = in.nextLine().split(" ");
        String command = input[0].toLowerCase();

        switch (command) {
        case "/nick":
            if (input.length >= 2) {
                nickname = input[1];//from  w  w  w. j a  v a  2s . c  o m
                System.out.println("your nickname is " + nickname);
            } else {
                System.out.println("write nick name please!");
            }
            break;
        case "/join":
            if (input.length >= 2) {
                KafkaConsumer consume = new KafkaConsumer(groupId, input[1]);
                consume.start();

                channelGroup.put(input[1], consume);
            } else {
                System.out.println("write channel name please!");
            }
            break;
        case "/leave":
            if (input.length >= 2) {
                if (channelGroup.containsKey(input[1])) {
                    KafkaConsumer consume = channelGroup.get(input[1]);
                    consume.shutdown();
                    channelGroup.remove(input[1]);
                    System.out.println("your leave channel " + input[1]);
                } else {
                    System.out.println("wrong channel name!!");
                }
            } else {
                System.out.println("write channel name please!");
            }
            break;
        case "/exit":
            System.out.println("bye");
            System.exit(0);
            break;
        default: //broadcast
            if (command.contains("@")) {
                String channelname = command.substring(1);
                KafkaProducer produce = new KafkaProducer();
                if (channelGroup.containsKey(channelname)) {
                    String message = "";
                    for (int i = 1; i < input.length; i++) {
                        message += input[i] + " ";
                    }
                    produce.sendMessage(channelname, message, nickname);
                }
                produce.closeConnection();
            } else {
                String message = "";
                for (int i = 0; i < input.length; i++) {
                    message += input[i] + " ";
                }

                KafkaProducer produce = new KafkaProducer();
                for (String cList : channelGroup.keySet()) {
                    produce.sendMessage(cList, message, nickname);
                }
                produce.closeConnection();
            }
        }
    }
}

From source file:math2605.gn_qua.java

/**
 * @param args the command line arguments
 *//*  w  w  w  .j av  a2 s  .  co m*/
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix(pairs.size(), 1);
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix(pairs.size(), 3);
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());
}

From source file:javaapplicationclientrest.JavaApplicationClientRest.java

/**
 * @param args the command line arguments
 *///ww  w . j a  v  a2  s . c  o m
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    ControllerCliente controller = new ControllerCliente();
    while (true) {
        controller.getMenu();

        int op = 0;
        try {
            op = Integer.parseInt(input.nextLine());
        } catch (Exception e) {
        }

        //input.nextLine();
        switch (op) {
        case 1:
            controller.listarNoticias();
            break;
        case 2:

            System.out.println("Informe o titulo: ");
            String titulo = input.nextLine();
            if (titulo.trim().equals("")) {
                System.out.println("Ttulo invlido");
                break;
            }
            System.out.println("Informe o autor: ");
            String autor = input.nextLine();
            if (autor.trim().equals("")) {
                System.out.println("Autor invlido");
                break;
            }
            System.out.println("Informe o Conteudo: ");
            String conteudo = input.nextLine();
            if (conteudo.trim().equals("")) {
                System.out.println("Conteudo invlido");
                break;
            }
            //System.out.println(id);
            controller.cadastrarNoticia(titulo, autor, conteudo);
            break;

        case 3:

            try {
                System.out.println("Informe o id da notcia a ser removida: ");
                String idN = input.nextLine();
                Integer.parseInt(idN);
                controller.removerNoticia(idN);
            } catch (Exception e) {
                System.out.println("Numero invlido");
            }
            break;
        case 4:
            try {
                System.out.println("Informe o Id da notcia desejada:");
                String idN = input.nextLine();
                Integer.parseInt(idN);
                controller.getNoticia(idN);
            } catch (Exception e) {
                System.out.println("Id invlido");
                break;
            }
            break;
        case 5:
            try {
                System.out.println("Informe o Id da notcia para ser atualizada:");
                String idA = input.nextLine();
                Integer.parseInt(idA);
                System.out.println("Informe o novo titulo: ");
                String tituloN = input.nextLine();
                controller.atualizarNoticia(idA, tituloN);
            } catch (Exception e) {
                System.out.println("Id invlido");
            }
            break;

        default:
            System.out.println("Opo invlida. Escolha uma opo novamente");
            break;
        } // End switch
    } // End while

}

From source file:com.mycompany.test.Jaroop.java

/**
 * This is the main program which will receive the request, calls required methods
 * and processes the response.//from  ww w. ja va  2s . c o m
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    Scanner scannedInput = new Scanner(System.in);
    String in = "";
    if (args.length == 0) {
        System.out.println("Enter the query");
        in = scannedInput.nextLine();
    } else {
        in = args[0];
    }
    in = in.toLowerCase().replaceAll("\\s+", "_");
    int httpStatus = checkInvalidInput(in);
    if (httpStatus == 0) {
        System.out.print("Not found");
        System.exit(0);
    }
    String url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles="
            + in;
    HttpURLConnection connection = getConnection(url);
    BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String request = "";
    StringBuilder response = new StringBuilder();
    while ((request = input.readLine()) != null) {
        //only appending what ever is required for JSON parsing and ignoring the rest
        response.append("{");
        //appending the key "extract" to the string so that the JSON parser can parse it's value,
        //also we don't need last 3 paranthesis in the response, excluding them as well.
        response.append(request.substring(request.indexOf("\"extract"), request.length() - 3));
    }
    parseJSON(response.toString());
}