8 Replies - 8438 Views - Last Post: 22 February 2011 - 12:26 AM Rate Topic: ***-- 2 Votes

#1 vikrant839  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-July 10

Multiple inputs in java

Posted 20 February 2011 - 10:27 AM

i have this problem i am working on, a part of which states as follows

"The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j."

could some one help me out and tell me that how can we input an unknown number multiple inputs dynamically.
or otherwise just help me understand this part of the question.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple inputs in java

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Multiple inputs in java

Posted 20 February 2011 - 10:32 AM

You can use a Scanner object. In it, there is a hasNext() method that will return true if there is more input to be processed and false if not. Use that in conjunction with next() and you will be good. If they're all integers, you can use the hasNextInt() and nextInt() methods.

while (scan.hasNextInt()) {
    int blah = nextInt();
    ...
}



Something like that.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multiple inputs in java

Posted 20 February 2011 - 01:46 PM

The assignment says a pair per line
So read until there is no more line
Extract the int with another scanner

File file = new File("data.txt");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
   String line = scan.nextLine();
   Scanner lineScanner = new Scanner(line);
   int x = lineScanner.nextInt();
   int y = lineScanner.nextInt();
}


This post has been edited by pbl: 20 February 2011 - 04:22 PM
Reason for edit:: File not file

Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Multiple inputs in java

Posted 20 February 2011 - 04:21 PM

View Postpbl, on 20 February 2011 - 08:46 PM, said:

The assignment says a pair per line
So read until there is no more line
Extract the int with another scanner

File file = new file("data.txt");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
   String line = scan.nextLine();
   Scanner lineScanner = new Scanner(line);
   int x = lineScanner.nextInt();
   int y = lineScanner.nextInt();
}



You mean File file = new File("data.txt");? :)

Oh and the try-catch block.
Was This Post Helpful? 1
  • +
  • -

#5 vikrant839  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-July 10

Re: Multiple inputs in java

Posted 21 February 2011 - 06:36 AM

hey pbl, thanx for help but you know i am new to java and i am not dealing with
usage of files right now. bummer. this is a little difficult for me to understand.

Dogstopper gave a good concept of hasnext() method, can you please explain me this ?


@dogstopper thanx for your contribution, i am looking forward to go with your choice,
plz can you help me with using hasnext()/ hasnextline() ?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10383
  • View blog
  • Posts: 38,430
  • Joined: 27-December 08

Re: Multiple inputs in java

Posted 21 February 2011 - 09:32 AM

Have you read the documentation? What don't you understand?
Was This Post Helpful? 0
  • +
  • -

#7 vikrant839  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-July 10

Re: Multiple inputs in java

Posted 21 February 2011 - 11:57 PM

i have read the documentation of the problem, the problem is by far clear to me, only this part i don't
understand of taking in multiple inputs from the user without predefining the number of inputs.i have figured out the logic for solving the problem, it is only this part which is keeping me from implementing
it. i don't know how to start the problem without knowing the input method.plz help me in this.
Was This Post Helpful? 0
  • +
  • -

#8 Raeghin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 07-May 10

Re: Multiple inputs in java

Posted 22 February 2011 - 12:16 AM

As pbl stated before, use one scanner to read each line and use another Scanner to extract the integers.

Instead of the first Scanner to read from a line, let it read from System.in (the console).

Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
   String line = scan.nextLine();
   Scanner lineScanner = new Scanner(line);
   int x = lineScanner.nextInt();
   int y = lineScanner.nextInt();
}



This will keep on reading the console input until the program is stopped.
Of course you can set another stop condition (like for instance a empty line).

Boolean continueScan = true;
Scanner scan = new Scanner(System.in);
while(continueScan) {
   String line = scan.nextLine();
   if(line.equals("")){
	   continueScan = false;
   }else{
	   Scanner lineScanner = new Scanner(line);
	   int x = lineScanner.nextInt();
	   int y = lineScanner.nextInt();
   }
}



There are probably prettier ways to achieve this, this is just a quick and dirty way.

For more information on the Scanner: http://download.orac...il/Scanner.html

This post has been edited by Raeghin: 22 February 2011 - 12:17 AM

Was This Post Helpful? 0
  • +
  • -

#9 vikrant839  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-July 10

Re: Multiple inputs in java

Posted 22 February 2011 - 12:26 AM

Thanx Raeghin, i look into it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1