#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    [JAVA] Reading Multiple Values


    I am requesting a little help & guidance with reading from a file with multiple values. The file will have approximately 50-60 values in total.

    I am wanting to be able to check ALL the values for an Integer, either 0, 1 or -1.

    Each value also has a unique ID.

    Here is an example I have for the 1st value, but with 50 different values, this could be 1 long piece of code! Any help reducing this would be greatly appreciated for up to 60 values.

    Code:
    			File questFile = new File("quest/" + user.toLowerCase() + ".txt");
    			Properties props = new Properties();
    			FileInputStream fis = new FileInputStream(questFile);
    			props.load(fis);
    			int content = Integer.parseInt(props.getProperty("firstValue"));
    			if(content == 0) {
    				questID = 4;
    				questStage = 0;
    			}
    			if(content >= 1) {
    				questID = 4;
    				questStage = 1;
    			}
    			if(content == -1) {
    				questID = 4;
    				questStage = -1;
    			}
    			s.addInt(questID);
    			s.addInt(questStage);
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The program should use a loop to read values from the file until there are no more values in the file.
    Each value also has a unique ID.
    If the file is a properties file, what are the "names" for all the values in the file? You will need a complete list of all the names to be able to access all the values. The Properties class has methods that will get help you get the list.
    Last edited by NormR; October 31st, 2013 at 07:21 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    The program should use a loop to read values from the file until there are no more values in the file.

    If the file is a properties file, what are the "names" for all the values in the file? You will need a complete list of all the names to be able to access all the values.
    It's basically a text file which reads as follows:

    Code:
    firstValue=0
    secondValue=0
    thirdValue=0
    And so on ... The 0 values will also different, determined on stages of a task, so it could be:

    Code:
    firstValue=0
    secondValue=1
    thirdValue=-1
    fourthValue=0
    Thank you for your reply, I greatly appreciate it.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Have you thought about using a Map where the keys are Integer and the value is an ArrayList?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by bullet
    Have you thought about using a Map where the keys are Integer and the value is an ArrayList?
    Is there any chance you could provide an example, please? Your reply is also greatly appreciated.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The Properties class has methods that will get help you get the list of keys.
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Here is an example of using the idea to read in a file and create a map where the keys are length of words, and the value is an ArrayList containing the words of that length from the file.

    Code:
    import java.io.*;
    import java.util.*;
    
    public class ReadFile {
    
       public static void main(String[] args) {
          try {
             Scanner scanner = new Scanner(new FileInputStream(args[0])).useDelimiter("\\Z");
    
             String line = scanner.next();
    
             scanner.close();
    
             String[] tokens = line.replaceAll("\\s+"," ").split(" ");
    
             Map<Integer,ArrayList<String>> map = new TreeMap<>();
    
             for (String token : tokens) {
                int length = token.length();
    
                ArrayList<String> words = map.get(length);
    
                if (words == null)
                   words = new ArrayList<>();
    
                if (!words.contains(token))
                   words.add(token);
    
                map.put(length,words);
             }
    
             for (int number : map.keySet()) {
                System.out.println("These are the words in the file of length " + number + ".\n");
    
                ArrayList<String> words = map.get(number);
    
                for (String word : words)
                   System.out.println(word);
    
                System.out.println("");
             }
    
          } catch (IOException | NoSuchElementException e) {
             System.out.println(e);
          }
    
       }
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo