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

    Default How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    Hello once again, I was unable to remove my other thread. If a moderator or admin could remove my other thread of the same name, I would be grateful.

    Currently I am trying to figure out how to convert an object to string value, then return that value to a string.

    My real intention is to get a value stored into stringthing in order to put that value into a combobox for a GUI I am making.
    stringthing would be put in there by doing
    Java Code:
    public static String anode0=CsvFileReader.stringthing;
    and then
    Java Code:
    jComboBox1.addItem(anode0);
    I have proved that this is totally legal and works when I do
    Java Code:
    public static  String stringthing="yes";
    "yes" will appear in the combobox if I define stringthing.

    what I want to do is convert student from an object to a string and return that string value to stringthing, such that whatever the "students" is when converted to string the value will be stored into stringthing and by extension be put into the combobox.

    Java Code:
    package evansinterface;
    import java.io.BufferedReader;
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import evansinterface.Student;
    import java.io.PrintStream;
    
    /**
     * @author 
     *
     */
    
    public class CsvFileReader {
    	
    	//Delimiter used in CSV file
    	public static  String COMMA_DELIMITER = ",";
            public static  String stringthing;
            //public static List<Object> students;
    	//Student attributes index
    	public static  int STUDENT_ID_IDX = 0;
    	public static  int STUDENT_FNAME_IDX = 1;
    	public static  int STUDENT_LNAME_IDX = 2;
    	public static  int STUDENT_GENDER = 3; 
    	public static  int STUDENT_AGE = 4;
            public static String Scanner;
    	
            
     
    //}
        /**readCsvFile
         *
         * @param fileName
         */
        public static String readCsvFile(String fileName) {
    
    		BufferedReader fileReader = null;
         
            try {
            	
            	//Create a new list of student to be filled by CSV file data 
            	List students = new ArrayList();
                String line = "";
                
                //Create the file reader
                fileReader = new BufferedReader(new FileReader(fileName));
                
                //Read the CSV file header to skip it
               
                fileReader.readLine();
                
                //Read the file line by line starting from the second line
                while ((line = fileReader.readLine()) != null) {
                    //Get all tokens available in line
                    String[] tokens = line.split(COMMA_DELIMITER);
                    if (tokens.length > 0) {
                    	//Create a new student object and fill his  data
    					Student student = new Student(Long.parseLong(tokens[STUDENT_ID_IDX]), tokens[STUDENT_FNAME_IDX], tokens[STUDENT_LNAME_IDX], tokens[STUDENT_GENDER], Integer.parseInt(tokens[STUDENT_AGE]));
    					students.add(student);
    				}
                }
                
                //Print the new student list
                for (Object student : students) {	
                stringthing = student.toString();
                System.out.println(stringthing);
                return stringthing;
                
               
                
          
    			}
            } 
            catch ( IOException | NumberFormatException e) {
            	System.out.println("Error in CsvFileReader !!!");
            } finally {
                try {
                    fileReader.close();
                } catch (IOException e) {
                	System.out.println("Error while closing fileReader !!!");
                }
            }
    return stringthing;
    	}
               public void takeNames(String fName){
    
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter your first and last name");
    fName = scanner.next();
    System.out.printf("Hello %s",Scanner); //return statement needed!
    
            }
    
        CsvFileReader() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    how to convert an object to string value,
    That depends on what values are in the object and how you want them represented in a String.
    The normal way to obtain a String from an object is to call its toString() method. The toString() method needs to be overridden to build a String representing the values in the object and then return that String.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    Quote Originally Posted by Norm View Post
    That depends on what values are in the object and how you want them represented in a String.
    The normal way to obtain a String from an object is to call its toString() method..
    I have converted to string on line 67. That general area with the for loop is where I'm kind of focusing on. I don't understand what you mean by overridden.
    The toString() method needs to be overridden to build a String representing the values in the object and then return that String.
    I have attempted putting return stringthing; on lines 69 and 85 but with no success. Does the value of stringthing just stay in the loop and not leave?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    what you mean by overridden.
    I was referring to the Override of an existing method in a class.

    Does the value of stringthing just stay in the loop and not leave?
    The return method will cause execution to exit a method. The code has a return statement inside of a loop (line 69) which means that the loop will stop looping when the return method is executed.
    If you want to build a long String inside of the loop and then return it after the loop finishes executing, use String concatenation (the + operator) inside of the loop to build the long String.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    Quote Originally Posted by Norm View Post
    I was referring to the Override of an existing method in a class.


    The return method will cause execution to exit a method. The code has a return statement inside of a loop (line 69) which means that the loop will stop looping when the return method is executed.
    If you want to build a long String inside of the loop and then return it after the loop finishes executing, use String concatenation (the + operator) inside of the loop to build the long String.
    I guess being a complete noob here I am confused about String concatenation (the + operator)
    I take it I'm supposed to put in something custom for (the + operator). However I don't really understand what the operator means. Am I supposed to refer to strings in there? Do I put this before or after return?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    what the operator means.
    That is a very basic computer programming term. Try doing a google search about what an operator is.
    Basically an expression is made of operands and an operator. For example the expression: 1 + 2 has two operands: 1 & 2 and the operator: +

    The String concatention operator: + is used to build a longer String: string1 + string2 gives a new String composed of what was in string1 and string2

    I suggest that you try writing a very small test program that has a method that returns a String and that uses a loop to build a long string using the + operator. Have the main() method call the test method, receive the String that is returned and print out its value.

    Trying to write such a large program with your limited knowledge of java is going to be tough and frustrating. Work on the programming constructs you need to understand by writing some small programs.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: How to return string value to string [PLEASE REMOVE DUPLICATE PRIOR TO THIS ONE]

    Yup, just started Java recently.

    Anyway I think I will take your advice in testing a few things out in smaller programs and getting the idea of how things work in Java a little better.
    Also thank you for sharing what operator and operand is.

Similar Threads

  1. Remove Duplicate words from String
    By Alijavaprogrmammer in forum New To Java
    Replies: 6
    Last Post: 12-26-2013, 12:24 AM
  2. Remove Duplicate words from String
    By Alijavaprogrmammer in forum String Codes
    Replies: 4
    Last Post: 12-24-2013, 10:20 PM
  3. How to find duplicate words in string array?
    By chkontog in forum New To Java
    Replies: 2
    Last Post: 11-12-2012, 01:16 AM
  4. remove duplicate word in the string
    By katturv in forum New To Java
    Replies: 7
    Last Post: 12-19-2010, 07:16 PM
  5. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM

Posting Permissions

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