Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2007, 11:34 PM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-02-2007, 12:08 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-02-2007, 12:28 AM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-02-2007, 12:38 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-02-2007, 01:00 AM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-02-2007, 01:07 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
You can use the Double class to convert it to a double:
Code:
Double.valueOf(String s);
That will return a double of the String you entered.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-02-2007, 01:10 AM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-02-2007, 01:22 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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:
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 01:24 AM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-02-2007, 01:58 AM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-02-2007, 02:05 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-02-2007, 03:26 AM
Member
 
Join Date: Dec 2007
Posts: 6
ducster is on a distinguished road
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:
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'); } }
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-03-2007, 05:08 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 04:32 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org