-2

I need a code to write String in the console and making compare with Array.

String Array = {"skill"};
Console co=System.console();

then compare Array with co...

sarot
  • 49
  • 1
  • 1
  • 4
  • 5
    The first line of your sample code doesn't compile, making it hard to tell what you're trying to achieve. – Jon Skeet Dec 12 '12 at 09:53
  • you right my English not good better must I say I tried to write code for inputing data by console and making compare with an Array. – sarot Dec 12 '12 at 09:59
  • Your **code** will not compile. It doesn't help that your English isn't good either - you haven't explained *how* you want to compare the user input with the array - but I was mostly commenting on the code... – Jon Skeet Dec 12 '12 at 10:01
  • Ok first can you tell me how I can input String by console ? – sarot Dec 12 '12 at 10:03
  • `System.in.readLine()`? Or `Console.readLine()`? How much of the `Console` API did you look at before asking us? – Jon Skeet Dec 12 '12 at 10:23
  • He wanted to ask something like this: `var stringArray = new[] { "skillFirst", "skillSecond" };` `var consoleInput = Console.ReadLine();` – MikroDel Dec 12 '12 at 10:42

1 Answers1

6

here is an example to read a string from the keyboard and compare the string with another string

import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        String input = null;
        String compareString = "hello world";
        Scanner inputReader = new Scanner(System.in);
        System.out.println("please enter: hello world" );

        // here is how you take the input from keyboard

        input = inputReader.nextLine();
                // this is how you write to console
        System.out.println("You entered :" + input);

        //Here is how you compare two string
        if(compareString.equals(input))
        {
            System.out.println("input is: hello world");
        }
        else
        {
            System.out.println("input is not: hello world");
        }
    }
}      

Execution1:

Please enter: hello world

Input:

hello world

output:

You entered :hello world
input is: hello world

Execution2:

Please enter: hello world

Input:

hello

output:

You entered :hello 
input is not: hello world
codeMan
  • 5,730
  • 3
  • 27
  • 51