6 Replies - 10338 Views - Last Post: 18 December 2009 - 11:04 AM Rate Topic: -----

#1 groman88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 09

input from keyboard on same line

Posted 18 December 2009 - 07:50 AM

This seems like something easy but just cant figure it out, im spacing out lol. How can I get
input from keyboard to show up on the same like like this

Enter three numbers: 1 2 3

I get
Enter three numbers: 1
2
3


each input is its own variable

System.out.print("Enter three numbers: );
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int num3 = keyboard.nextInt();
:ph34r:

Is This A Good Question/Topic? 0
  • +

Replies To: input from keyboard on same line

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: input from keyboard on same line

Posted 18 December 2009 - 08:26 AM

System.out.println(num1 +" " + num2 + " " + num2); //" " is added for a space


Sorry, misread your post. Are you trying to get three seperate numbers or one number with 3 digits? Just wondering why you need them all on one line.

This post has been edited by nick2price: 18 December 2009 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 groman88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 09

Re: input from keyboard on same line

Posted 18 December 2009 - 08:47 AM

View Postnick2price, on 18 Dec, 2009 - 07:26 AM, said:

System.out.println(num1 +" " + num2 + " " + num2); //" " is added for a space


Sorry, misread your post. Are you trying to get three seperate numbers or one number with 3 digits? Just wondering why you need them all on one line.



Oh i meant like when I ask the user to enter 3 numbers and the inputs show up on the same line. Each number is gonna be saved in a different variable like num1, num2, num3,

So I do

System.out.print("Enter three numbers: ");
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int num3 = keyboard.nextInt();

the user input (in bold) comes out looking like this

Enter three numbers: 1
2
3


I dont want the 2 and 3 on there own line when user inputs them , I want them so when the user inputs the numbers they show up on the same line next to the 1 like this

Enter three numbers: 1 2 3
Was This Post Helpful? 0
  • +
  • -

#4 Diomedes  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 23
  • Joined: 16-June 09

Re: input from keyboard on same line

Posted 18 December 2009 - 09:08 AM

View Postgroman88, on 18 Dec, 2009 - 07:47 AM, said:

View Postnick2price, on 18 Dec, 2009 - 07:26 AM, said:

System.out.println(num1 +" " + num2 + " " + num2); //" " is added for a space


Sorry, misread your post. Are you trying to get three seperate numbers or one number with 3 digits? Just wondering why you need them all on one line.



Oh i meant like when I ask the user to enter 3 numbers and the inputs show up on the same line. Each number is gonna be saved in a different variable like num1, num2, num3,

So I do

System.out.print("Enter three numbers: ");
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
int num3 = keyboard.nextInt();

the user input (in bold) comes out looking like this

Enter three numbers: 1
2
3


I dont want the 2 and 3 on there own line when user inputs them , I want them so when the user inputs the numbers they show up on the same line next to the 1 like this

Enter three numbers: 1 2 3



I think what would help you is to read an entire line of input from the user, then parse the input by using spaces. That way the user enters 3 numbers and presses enter once (i.e. it's all on the same line) while you check the user's input and break it up.

ex:
	Console console = System.console();
	String numbers = console.readLine("Enter three numbers: ");

	//code to break string into 3 strings and cast into ints, checking input for validity along the way.


Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: input from keyboard on same line

Posted 18 December 2009 - 09:09 AM

ok, from the scanner api

Quote

The scanner can also use delimiters other than whitespace. This example reads several items in from a string:

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();


See if you can figure somthing out from this.
Was This Post Helpful? 0
  • +
  • -

#6 groman88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-December 09

Re: input from keyboard on same line

Posted 18 December 2009 - 09:11 AM

Ok guys thanks for the input!
Was This Post Helpful? 0
  • +
  • -

#7 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: input from keyboard on same line

Posted 18 December 2009 - 11:04 AM

try to use
 Scanner scan=new Scanner(System.in)
scan.next();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1