Assigning variables to columns in a file

isames
isames used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Top Expert 2016
Commented:
You're lucky, it's tab-separated, so  try

CredData = new Scanner(new File("credentials.txt")); //Opens the file.
CredData.useDelimiter("\t+|[\r\n]+"); // delimiters are tabs and linefeeds

Open in new window


btw, that should be credData as Java variable names begin lower case
>> 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!!
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial