Results 1 to 12 of 12
  1. #1
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default Help with Java project

    Hey everyone,
    I am new here and am in a Java class in college. I am trying to make a basic currency converter with a file that has country and currency rates. I want the program to look up the rates by finding the country. So far this is what I have
    Java Code:
    import java.util.*;
     
    import java.io.*;
     
    import java.util.Scanner;
     
    public class converter {
     
    double usdollars;
     
    String country;
     
    double exchangerate;
     
    String countrym;
     
    public static void main(String[] args) {
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Hello");
     
    System.out.println("Would you like to convert(c) money today, modify(m) or exit(x)");
     
    String answer = keyboard.nextLine();
     
    char answer1 = answer.charAt(0);
     
    while (answer1 != 'x')
     
    {
     
          if (answer1 == 'c')
     
    {	
     
          System.out.println("How many U.S. Dollars?");
     
          double usdollars = keyboard.nextDouble();
     
          System.out.println("What countries currency do you want to exchange for? (3 letter country code)");
     
    try
     
    {
     
          BufferedReader inputStream = new BufferedReader(new FileReader("rates"));
     
          String country = keyboard.nextLine();
     
    while (country != null)
     
    {
           country = inputStream.readLine();
           System.out.println(country);
     
    }
           inputStream.close();
    }
     
    catch(FileNotFoundException e)
     
    {
     
          System.out.println("error opening rates.");
     
           System.exit(0);
     
    }
     
    catch(IOException e)
     
    {
     
    System.out.println("Error reading from file rates.");
     
    }
    }
     
    else if (answer1 == 'm')
     
    {
     
          System.out.println("What country do you want to modify?");
     
          String countrym = keyboard.nextLine();
     
          System.out.println("What is the new exchange rate?");
     
          double exchangerate = keyboard.nextDouble();
     
    }
     
    else if (answer1 == 'x')
     
    {
     
    System.exit(0);
     
    }
     
    else
     
    {
     
    System.out.println("Invalid option");
     
    }
     
    answer = keyboard.nextLine();
     
    answer1 = answer.charAt(0);
     
    }
     
    }
    }
    I keep getting this error message
    java.io.FileNotFoundException: rates.rtf (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at java.io.FileReader.<init>(FileReader.java:41)
    at converter.main(converter.java:50)

    I am not to sure what it is trying to tell me
    but any help would be greatly apreciated and any tips would be helpful also.
    Thanks
    Dan

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    BufferedReader inputStream = new BufferedReader(new FileReader("rates"));
    java.io.FileNotFoundException: rates.rtf (No such file or directory)
    Is telling you that it cannot find the file rates.

  3. #3
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the quick reply
    I put the file in a new folder and i no longer get the error posted above instead i get this:
    {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf440
    {\fonttbl\f0\fswiss\fcharset77 Helvetica;}
    {\colortbl;\red255\green255\blue255;}
    \vieww9000\viewh8400\viewkind0
    \deftab720
    \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5 040\tx5760\tx6480\tx7200\tx7920\tx8640\pardeftab72 0\ql\qnatural\pardirnatural

    \f0\fs24 \cf0 cad .9995\
    }
    null

    I am able to move on from their and type in a country I want but it just goes into a loop asking for US money again in a loop
    Thanks
    Dan

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    That file is a rich text document (rtf) meaning that you probably wrote it in WordPad, or some other tool. Make that file with Notepad and save with a .txt extension.

    It keeps looping because it cant find any countries in that gibberish output you get from rich text format.

  5. #5
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    alright I stopped being stupid and downloaded a simple text editor to create a simple text document. I got it to read what I wanted but now i need to figure out how to get it to read .9995 and convert it from a string to a double or something similiar to multiply it against usdollars. Any suggestions would be appreciated.

    Again sorry for my noobishness

    Thanks
    Dan

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    You can use the Double class to convert it to a double:
    Java Code:
    Double.valueOf(String s);
    That will return a double of the String you entered.

  7. #7
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help but i think i have the coding messed up in this section
    try

    {

    BufferedReader inputStream = new BufferedReader(
    new FileReader("rates.c"));

    String country = keyboard.nextLine();

    while (country != null)

    {
    country = inputStream.readLine();
    System.out.println(country);

    }

    inputStream.close();
    }


    It just automatically reads the file and doesn't wait for me to enter a country. Do you have any suggestions for that?

    And thank you so much for your help

    Dan

  8. #8
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    You are reading it line by line, and setting the country variable to whatever line you have. You should put it in an array, or an ArrayList, then compare the contents of the file, with the input. Something like this:
    Java Code:
    	BufferedReader rd = new BufferedReader(new FileReader("test.txt"));
    	String country;
    
    	ArrayList<String> file = new ArrayList<String>();
    	while ((country = rd.readLine()) != null) {
    		file.add(country);
    	}
    
    	for (String s : file) {
    		if (s.equals(keyboard.nextLine())) {
    			System.out.println("i know of this contry");
    		}else {
    			System.out.println("I dont know thic contry");
    		}
    	}
    Last edited by staykovmarin; 12-02-2007 at 12:24 AM.

  9. #9
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help

    So do you think it would be easier if I had multiple files each one being a country which the file would only have that countries rate in it and the user types in the country to specify which file? Does that make sense?

    Because what you did seems to just verify the country is there correct or am i miss reading that?

    thank
    dan

  10. #10
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    The countries are in one file, what you are doing is reading the file, you are just not storing the information correctly.

    But yes you could have an individual file, but that seems harder for me.

    Read up on ArrayLists. what i did for you above is add the entire file into an arraylist, line by line. You can use the for each statement to read trough it till you find a match.

  11. #11
    ducster is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    Hey guys,

    I am almost there I can get it to work but it loops back around to "How many U.S. Dollars?" How do I get that to not do that but instead ask System.out
    .println("Would you like to convert(c) money today, modify(m) or exit(x)"); again?

    Here is my code thus far:
    Java Code:
    import java.util.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class converter {
    
    	double usdollars;
    
    	String country;
    
    	double exchangerate;
    
    	String countrym;
    
    	public static void main(String[] args) {
    
    		Scanner keyboard = new Scanner(System.in);
    
    		System.out.println("Hello");
    
    		System.out
    				.println("Would you like to convert(c) money today, modify(m) or exit(x)");
    
    		String answer = keyboard.nextLine();
    
    		char answer1 = answer.charAt(0);
    
    		do
    		{	
    		
    		
    
    		{
    
    			if (answer1 == 'c')
    
    			{
    
    				System.out.println("How many U.S. Dollars?");
    
    				double usdollars = keyboard.nextDouble();
    
    				System.out
    						.println("What countries currency do you want to exchange for?");
    				String country = keyboard.next();
    				country.toLowerCase();
    				
    				try
    
    				{
    
    					BufferedReader inputStream = new BufferedReader(
    							new FileReader(country));
    	
    							String country1 = null;
    							country1 = inputStream.readLine();
    							double rate = Double.parseDouble(country1);
    					        System.out.println(rate);
    
    							double converted; 
    							converted = usdollars * rate;
    							System.out.print("You would have " );
    							System.out.println(converted);
    
    					inputStream.close();
    				}
    
    				catch (FileNotFoundException e)
    
    				{
    
    					System.out.println("error opening rates.");
    					e.printStackTrace();
    					System.exit(0);
    
    				}
    
    				catch (IOException e)
    
    				{
    
    					System.out.println("No rate for that country.");
    
    				}
    				
    				
    				
    			}
    
    			else if (answer1 == 'm')
    
    			{
    
    				System.out.println("What country do you want to modify?");
    
    				String countrym = keyboard.nextLine();
    
    				System.out.println("What is the new exchange rate?");
    
    				double exchangerate = keyboard.nextDouble();
    
    			}
    
    			else if (answer1 == 'x')
    
    			{
    
    				System.exit(0);
    
    			}
    
    			else
    
    			{
    
    				System.out.println("Invalid option");
    
    			}
    
    			
    
    		}
    		
    
    		}while (answer1 != 'x');
    	}
    }

  12. #12
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    		String answer = keyboard.nextLine();
    		char answer1 = answer.charAt(0);
    That code is outside the do-while loop. That makes it so answer1 never has a chance to change.

Similar Threads

  1. help for payroll project in java
    By mageshwari in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 03:46 AM
  2. UML to Java project
    By banie in forum NetBeans
    Replies: 3
    Last Post: 01-28-2008, 11:16 AM
  3. Help Java project.
    By mandrake446 in forum New To Java
    Replies: 1
    Last Post: 11-27-2007, 12:52 AM
  4. Java Project Help
    By Gambit17 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 12:53 PM
  5. Building A Java Project In Eclipse
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-22-2007, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •