Results 1 to 4 of 4
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default 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:

    Java Code:
    John 18
    Richard 22
    Harry 21
    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:
    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;
    
    }
    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.

    Thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    Thats great, i'll have a look at them and see what I can find.

    Thank you

Similar Threads

  1. Scanner Not working
    By Xystus777 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 12:22 AM
  2. Scanner-While Loop
    By hyunski in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:15 AM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 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
  •