Jump to content

how to scan keyboard input

- - - - -

  • Please log in to reply
2 replies to this topic

#1
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
hey guys im new to java(just starting it in uni)
i need some help in reading keyboard input.

i've got this so far:

int i;

int count;

System.out.println("Enter a number: \n");

//this is where the scan is ment to be

if((count<0 || count count >50

System.out.println("error.");

else

{

for(i=0;i<count;i++);

{

System.out.println("count is:") //how do i call the value of the i int the print?

}

}


also would a switch case statement work the same as in c?

thanks in advance
pindo

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
taking input:

Scanner scanner = new Scanner(System.in); // import java.util.Scanner if it complains

System.out.println("type something");

String something = scanner.nextLine();

System.out.println("you typed: " + something);

System.out.println("give number: ");

int number = scanner.nextInt();


Sample switch case statement:

int number = 3;

switch(number){

    case 1: System.out.println("The number is 1"); break;

    case 2: System.out.println("The number is 2"); break;

    case 3: System.out.println("The number is 3"); break;

    default: System.out.println("No idea what the number is, it's not 1, 2 or 3");

}



#3
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
Thank you for your help:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users