troubleshooting Question

Assigning variables to columns in a file

Avatar of isames
isames asked on
Java
7 Comments1 Solution98 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros