Results 1 to 4 of 4
Thread: Need help with scanner.
- 07-27-2009, 09:04 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Need help with scanner.
Hello Java Forums, I need your help once again ^^
I have finally given up with this. I know exactly what is going wrong but I can't work out a solution. I have a text file called names.txt with the following information:
I need to make a method that when sent the word "John" will return the number "18". So far I have been trying to do this using scanner without much luck. Here is the code I have so far:Java Code:John 18 Richard 22 Harry 21
I think I know why it isnt working, but It's hard to explain. I have tried all different combinations including a second loop inside. The more I think about it I dont know if this approach is even possible. If this way is too messy and complicated, what alternatives do I have? I was thinking of either using a SQL database or using array lists.Java Code:public double search (String nameEnter){ double age = 0; File fis = new File("C:/.....names.txt"); try{ Scanner sc = new Scanner(fis); while (sc.hasNext()) { if (sc.hasNext(nameEnter)) { if (sc.hasNextDouble()){ age = sc.nextDouble(); } } String draft = new String(); draft = sc.next(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return age; }
Thank you
-
This sounds like you need to use a HashMap<String, Integer> collection. This works like a dictionary. You look up the String "John" and it should return 18 if this pair was entered.
- 07-28-2009, 02:19 AM #3
HashMap API & examples
Here are some links to help you....
HashMap API = HashMap (Java Platform SE 6)
HashMap example = Java HashMap Example
ANother example = Creating a Hash Table (Java Developers Almanac Example)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 07-28-2009, 02:11 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Scanner Not working
By Xystus777 in forum New To JavaReplies: 5Last Post: 03-18-2009, 12:22 AM -
Scanner-While Loop
By hyunski in forum New To JavaReplies: 2Last Post: 03-12-2009, 02:15 AM -
Scanner
By choko in forum New To JavaReplies: 10Last Post: 01-24-2009, 03:37 PM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 01:41 PM -
help with IP scanner
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks