hi, i actually had done similar programs many times and don't know why eclipse gave me strange output this time. anything possibly wrong? thanks

class RobotShop
{
	public static void main(String[] args)
	{
		try
		{
			Scanner reader = new Scanner(new File("air.txt"));
			
			String robotName = null;
			String serial = null;
			double price = 0.00;
			double flyingSpeed = 0.00;
			String airWeapon= null;
			
			while(reader.hasNextLine())
			{
				robotName = reader.nextLine();
				System.out.println(robotName);
				serial = reader.nextLine();
				System.out.println(serial);
				price = reader.nextDouble();
				System.out.println(price);
				flyingSpeed = reader.nextDouble();
				System.out.println(flyingSpeed);
				airWeapon= reader.nextLine();
				System.out.println(airWeapon);
				
				//FlyingRobot fly = new FlyingRobot(robotName, serial, price, flyingSpeed, airWeapon);
				//fly.showInfo();
			}
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not found");
		}
		
		/*
		catch(InputMismatchException e)
		{
			System.out.println("Input miss match Exception");
		}
		*/
		
	}
}

my text file is as follow

Black Hawk
A777
7000
150.00
Missle
Killer 571
U571
10000
500
Missle
Air Tran
T389
3000
400
M16

my output is :

Black Hawk
A777
7000.0
150.0
            // what made this spaced out???? 
Missle
Killer 571
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextDouble(Unknown Source)
	at RobotShop.main(Robot.java:85)

Now what I am **guessing** is the following chunk of code could have caused the problem:-

flyingSpeed = reader.nextDouble();
System.out.println(flyingSpeed);
airWeapon= reader.nextLine();

Now before the first line is called I guess the scanner is at this point in the file:-

150.00
^
Missle
Killer 571
U571
10000
500
Missle

When we read the double from the file, the we move to the end of the current line as here:-

150.00
      ^
Missle
Killer 571
U571
10000
500
Missle

After that when nextLine() is called the scanner returns whatever it finds on the current line (between the current position and the end of line) which is nothing so it just returns a blank string.
Hence the subsequent System.out.println(airWeapon); just prints a blank line.
Now at the begining of the next loop the following is the position on the Scanner in your file:-

150.00
Missle
^
Killer 571
U571
10000
500
Missle

Now when you robotName = reader.nextLine(); , you actually read the "Missle" and not "Killer 571" as you wanted to, however this wouldn't cause a problem since both are Strings. The same also happens with serial = reader.nextLine(); , it actually reads the robot name "Killer 571" instead of "U571" but that too as fine as both are Strings.

Now when you try to read the "price" the problem arrives. You expect a double value but ... you Scanner is currently pointed on "U571". Because of which you get an InputMismatchException indicating that your program was expecting a double but found a String instead.

Hope this should clear it up.

Stephen,
yes, i was thinking about the similar thing. and after different attempts i figured it out that it was the reader.nextLine() caused the problem.
if i made my text doc all single string like:

Hawk
U571
7000
150.00
Missle
Killer
U571
10000
500
Missle
Trans
T389
3000
400
M16

it and just just

read.next()

**absolutely without

reader.nextLine()

then no problem.

the problem now is... if i have a string like "black hawk" in a line , how do i read it? it would mess it up if i use nextLine()... any suggestion? thanks.

by the way it is now

while(reader.hasNextLine())
			{
				String robotName = reader.next();
				System.out.println(robotName);
				String serial = reader.next();
				System.out.println(serial);
				double price = reader.nextDouble();
				System.out.println(price);
				double flyingSpeed = reader.nextDouble();
				System.out.println(flyingSpeed);
				String airWeapon= reader.next();
				System.out.println(airWeapon);
			}

My Suggestion would be the reverse of what you have opted for now, use nextLine() for all your tokens, inclusive of the double values.
You can use Double.parseDouble() to get the corresponsing double value from the String that is returned from nextLine().

My Suggestion would be the reverse of what you have opted for now, use nextLine() for all your tokens, inclusive of the double values.
You can use Double.parseDouble() to get the corresponsing double value from the String that is returned from nextLine().

thanks stephen,
however, using all nextLine() probably wouldn't be as realistic even though in this case it should work. however, nextLine() read directly the next line down below and that means if i have a blank line it will read it and take it as piece of data. umm, i think the reason why i never had problem was because i never had more than 2 word string in my text before.

thanks and nothing is in hurry, i am just playing around the Scanner class. if you come up with something that can read something like this:

Hawk two word 
U571
7000
150.00
Missle

Killer all together
K112
10000
500
Missle

Trans should come in one line
T389
3000
400
M16



Super
S999
20000
800
Nuke

that would be great. thank you thank you thank you

The problem is that nextDouble does not advance past the newline. You can use the methods in any order that works for you; none of the methods is necessarily better or worse, it depends on your situation. For example, you could continue to use nextDouble in your case, and simply add an extra "nextLine" to eat up the newline.

The idea BestJewSinceJC recommended is pretty good too, just add an extra "nextLine()" after your "nextDouble()" calls and your files should be read with no problem at all.

However to read the file in the second format you have mentioned, I suggest setting the delimitter in your Scanner to the new line character and use the next() method to read the text from the file.
You can check the javadocs of the Scanner class here on how to change your delimiter.
Also you will need some help regular expressions which you can get here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.