Reading an Input File!?

Cant figure out how to read it right

Page 1 of 1

1 Replies - 1024 Views - Last Post: 28 April 2009 - 09:48 AM Rate Topic: -----

#1 jaybfox4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-April 09

Reading an Input File!?

Posted 28 April 2009 - 09:14 AM

Okay so i am having trouble reading reading an input file a certain way.

her is a sample of a input file i havet.

2.5 3

3 three
3.0 5

***well first off my goal is to read the input file and if on one line there is only one number like 2.5 then i store that one so i can send it to my circle class as the radius. Then if the line has two integers, then i store them both and send both of them to my rectangle class as length, width.

I am having trouble reading the input file a distinguishing how to read a line if it has one integer or two and so on. I know how to read each double and then send it to my circle class but when it comes to two integers im not getting it to work.

So this is what i have got so far and im working on it.

Scanner console = new Scanner(;
Scanner input = new Scanner(new File ("Numbers.txt"));
while(input.hasNextLine()) {
	double line = input.nextDouble();
	shape s1 = new Circle(line);
	if(input.hasNextDouble()) {
		 double d = input.hasNextDouble();
		 Shape r1 = new Rectangle(line, d);
		 //(here i am trying to read if there is another interger on the line
		  //and i dont know what to do to make this thing work)


This code reads each double and then sends it to my circle class, but i want it to read a line that has two integers so i can send those to my rectangle class. (and also deal with the string in there *3 three* and throw it out)

I have been trying a bunch of different thing but i jsut cant get it to read the input file right. i

So hopefully some people can help me out with this please. Thanks

This post has been edited by jaybfox4: 28 April 2009 - 09:15 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Reading an Input File!?

#2 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Reading an Input File!?

Posted 28 April 2009 - 09:48 AM

where have you placed your text file?

in your project directory(where src classes is) make a folder called files

Scanner input = new Scanner(new File("../files/input.txt"));

id also change the name from input to fileinput just makes it alot clearer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1