read txt file and give out put

the code should read the txt file and then calculate the cost of the g

Page 1 of 1

1 Replies - 1506 Views - Last Post: 17 October 2009 - 10:50 AM Rate Topic: -----

#1 maxspgrg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-April 09

read txt file and give out put

Posted 17 October 2009 - 01:13 AM

/**I have posted the txt file as well but if i doesnot show up it is as below
Moretti 407 S 5.7
Lauscha 335 O 8.6
Vertroford 907 L 2.1

name, type number, type of glass, ounces (16 ounces = 1 pound).

S (special) = $10 per pound
O (opaque) = $12 per pound
L (Odd Lot) = $45 per pound

*/


import java.io.*;
import java.util.Scanner;

public class GlassCost
{
	public static void main (String[] args)throws IOException
	{
		String input, str, name;
		int num;
		char letter;
		double ounces, cost;
		
		
		Scanner keyboard = new Scanner (System.in);
		
		System.out.println("File name: ");
		input = keyboard.nextLine();
		
Scanner inFile = new Scanner (new FileReader ("and.txt"));
	
		
		name = inFile.nextLine();

		num = inFile.nextInt();
		
		while(inFile.hasNext())
		{
		letter = inFile.charAt(name);
		ounces = inFile.nextDouble();
		switch (letter)
		{
		case S:
		cost = (ounces/ 16) * 15;
		System.out.println(name + " uses Special costing: " + cost);
		break;
		case O:
								cost = (ounces/ 16) * 12;
		System.out.println(name + " uses Special costing: " + cost);
		break;
		case L:
		cost = (ounces/ 16) * 45;
		System.out.println(name + " uses Special costing: " + cost);
		break;
				
		}
		}
		
			inFile.close();
	}
}		

//I just need help to read the file properply please help


*Edit: Please use code tags in the future, thanks! :code:

Attached File(s)

  • Attached File  and.txt (58bytes)
    Number of downloads: 69

This post has been edited by Martyr2: 17 October 2009 - 10:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: read txt file and give out put

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4320
  • View blog
  • Posts: 12,106
  • Joined: 18-April 07

Re: read txt file and give out put

Posted 17 October 2009 - 10:50 AM

Well the main goals should be to first read in the whole line, next break that line apart into each of its pieces, convert those pieces to the appropriate type (string pieces which are numbers be converted to numbers) and lastly use these new pieces to make decisions.

The first part means we just have to use Scanner's nextLine to read in each line using a loop...

Scanner inFile = new Scanner (new FileReader ("and.txt"));
   
String line = "";
while(inFile.hasNextLine())
{
	 // Read in the line
	 line = inFile.nextLine();
}



So now that we have each line from the file being read into a variable called "line" we can operate on that line and break it apart. How we do this can be many different ways. We can split it using the string's split method, we could use substring() method to pull out the text we want or we could tokenize it using Stringtokenizer. We need to get each individual piece from the string. Once we have the pieces, any piece which is a number will need to be converted to an integer or double or whatever.

From there you will be able to make your calculations based on the pieces. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1