Results 1 to 5 of 5
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Use delimiter to get separate strings to put into combobox?

    Hello all,


    Once again I'm trying to tell Java to do something with a .csv file. What I am trying to do is read the text from a CSV file, and use the " , " as a delimiter which will separate the string values. I currently am Using demo code for reading from a .csv file and using a delimiter, but the problem is that it doesn't seem to be working. Is there some syntax error that I am not realizing?
    The code I found was for using with double datatypes.
    Java Code:
      private String[] dataFileToString(File inFile) throws FileNotFoundException{
            String[] data = new String[50];
            String lastData;
            int i = 0;
            
            try {
                FileReader readIn = new FileReader(templateFile);// take in filename
                try (Scanner src = new Scanner(templateFile) //scan the file
                ) {
                    src.useDelimiter(", *"); //every time there's a comma with a space, split the string
                    while (src.hasNext()) { //while you move to the next line
                        if (src.hasNext()) { // if there's another double
                            System.out.println(src.hasNext()); // print the double with the delimiter
                        
                        } else { 
                           System.out.println("UH OH");//otherwise
                            break;//leave the statement
                        }
                    }    
                    //readIn.close;
                } //every time there's a comma with a space, split the string
      
    
                BufferedReader readFile = new BufferedReader(readIn);
                
                lastData = readFile.readLine();
              // System.out.println(src+"\n");
               // System.out.println("THIS IS A LINE");  //this is the area I was focusing on 10/22/15
                jComboBox1.addItem(readFile);
                while(lastData != null && lastData != ""){
                    
                    if(i >= data.length){
                        
                        String[] temp = new String[data.length + 1];
                        
                        for(int j = 0; j < data.length; j++){
                            temp[j] = data[j];
                           // System.out.printf(data[0]+"\n");
                             
                        }
                        
                        data = temp;
                        
                    }
                    
                    
                    data[i] = lastData;
                    lastData = readFile.readLine();
                    i++;
                }    
                
            } catch (IOException ex) {
            }
            
            return data;
        }
    From what I am able to tell through the console is that the application DOES READ THE FILE but does not seem to use the delimiter.

    Am I doing something wrong?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Use delimiter to get separate strings to put into combobox?

    Well, hasNext doesn't print a double. It only prints true or false. Where do you read the actual value?

    Also, your while loop will never terminate because hasNext continuously returns true.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: Use delimiter to get separate strings to put into combobox?

    Quote Originally Posted by jim829 View Post
    Well, hasNext doesn't print a double. It only prints true or false. Where do you read the actual value?

    Also, your while loop will never terminate because hasNext continuously returns true.

    Regards,
    Jim
    You're right, hasNext doesn't print a double. What I want to do is print a string. hasNext for doubles would be something like hasNextDouble(). What I am trying to do is something like hasNextString , but as far as I'm aware that doesn't exist.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Use delimiter to get separate strings to put into combobox?

    What you want to do is use either hasNext() or hasNextLine() with next() or nextLine() respectively. The next() method will return the next token
    separated by the delimiter. Its return type is a String. The nextLine() will read the entire line. Its return type is also a String.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: Use delimiter to get separate strings to put into combobox?

    Quote Originally Posted by jim829 View Post
    what you want to do is use either hasnext() or hasnextline() with next() or nextline() respectively. The next() method will return the next token
    separated by the delimiter. Its return type is a string. The nextline() will read the entire line. Its return type is also a string.

    Regards,
    jim

    thank you so much it worked!!!

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2015, 09:21 AM
  2. Scanner Delimiter
    By Games2Design in forum New To Java
    Replies: 1
    Last Post: 03-22-2013, 05:16 PM
  3. Problem: Fill a comboBox from another combobox
    By Ridha in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2013, 02:34 PM
  4. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •