We help IT Professionals succeed at work.

Assigning variables to columns in a file

isames
isames asked
on
91 Views
Last Modified: 2019-02-23
When displaying the contents of the credential file, I'm trying to apply each column of data to a variable. For example:


a = griffen.keyes

b = 108....

c = alphabet soup

d = zookeeper


The "c " variable it's not working correctly. Java is seeing this as two values despite the "".

How do I fix that?

Attached is a screenshot that I am reading from.
Below is my code so far.





package openCredFile;
import java.util.*;
import java.io.*;

public class TestingRandomMethods {
   public static void main(String[] args)throws IOException{
       TestingRandomMethods T = new TestingRandomMethods();  
       T.openFile();
       T.readFile(); //The main method calls these methods and executes them. It's the readFile method that prints the info.
       T.closeFile();
       
}
       
    Scanner CredData; //Putting this outside of the method makes it available to all methods?
    public  void openFile() throws IOException {    //I had an error until I added "throws IOException" when trying to open the file.
         
        try{
        CredData = new Scanner(new File("credentials.txt")); //Opens the file.
    }
        catch(Exception e){
            System.out.println("Could not find file.");
        }
        }
    public void readFile(){
        while(CredData.hasNext()){ //will loop until it reaches the end of the file. hasNext means to read the entire file.
            String a = CredData.next();
            String b = CredData.next();
            String c = CredData.next();
            String d = CredData.next();
            //String e = CredData.next();
            //String f = CredData.next();
           
            //System.out.printf("%s %s %s %s \n", a,b,c,d); //I put in the format and the variables that I want to print.
            System.out.println(a);
            System.out.println(b);
       
        }
      }
    public void closeFile(){
        CredData.close();
    }
    }
credentials.txt
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
System.out.printf("%s %s %s %s \n", a,b,c,d); 

Open in new window


The only lines in that file with four tokens are the last two. The rest have five

Author

Commented:
I don't understand what you are talking about. I am just starting to learn Java. Can you elaborate for me?

Author

Commented:
Ok. I get what you are saying, that's why I posed the question.

How do I make the compiler read "alphabet soup" in the doc as one token?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
>> credData as Java variable names begin lower case

Since you are just learning Java, I'd like to clarify that this is not a language rule but just a convention so that when code is passed from one developer to another, they will be able to pick up the code organization a little faster. Here is a link that gives more of the Code conventions.

https://www.oracle.com/technetwork/java/codeconventions-135099.html

Author

Commented:
Thanks!!
CERTIFIED EXPERT
Top Expert 2016

Commented:
:)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.