Link to home
Start Free TrialLog in
Avatar of isames
isames

asked on

Assigning variables to columns in a file

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of isames
isames

ASKER

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

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
Avatar of isames

ASKER

Thanks!!
:)