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
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

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
isames

ASKER
I don't understand what you are talking about. I am just starting to learn Java. Can you elaborate for me?
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?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
phoffric

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

ASKER
Thanks!!
CEHJ

:)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.