CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2007
    Posts
    55

    Talking Using Split function, new to java

    ok im COMPLETELY new to java. but im just trying to do a psuedo-simple program (lol for me anyways)
    neways here's my code
    Code:
    //Java Program for CS-216
    //Program 5
    import java.io.*;
    import java.util.*;
    class test
    {
    
    	public static void main(String[] args)
    	{
    
    		String ID, LN, FN, MAJOR, YEAR;
    		String COURSE, SECTION, ENROLLED, ROOM, COURSEID;
    		String LINE;
    		String[] array = new String[5];
    		String test = "111222333";
    		String option;
    		try
    		{
    			BufferedReader in = new BufferedReader(new FileReader("student.data"));
    
    
    			while ((LINE = in.readLine()) != null)
    			{
    				array=LINE.split("/s");
    				if (array[0] == test)
    				{
    					System.out.println("string exists GJ buddy!");
    				}
    			}
    
    		}
    		catch (IOException e)
    		{
    			System.out.println(e);
    		}
    		
    	}
    }
    I know i know, not very impressive but hey its my first time.

    anyways basically Im just using it to test right now.

    it's reading a file that looks like this
    111111111 Adams Sally CS FR
    111222333 Samuels Ann EE SO
    333444555 Charels Don CS SO
    888777666 Daniels Tom UN JU
    555666777 Martin Al CE SE
    555444333 Adams Sally EE FR
    777888999 Nelson Mary CS SO
    999888777 Dinh Tran MA JU
    000111222 Galvez Mara UN SE
    777999888 Martin Dan CS FR
    123456789 Adams George CS SO
    888888888 Smith John CS JU

    basically i want it to read in a line
    like this: 111111111 Adams Sally CS FR
    then split it into "ARRAY" ^^ like I have up top
    then check the FIRST thing
    so like the 9 digit "id" number

    I put a test number "111222333"
    but when i go to check if it exists I get nothing.

    any help. i know it's something simple.

    thx all. and keep in mind this is my first time with java lol?

    is my split regexpression identifier wrong maybe?
    any help though would be appreciated. THX

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Split function, new to java

    You're comparing the two Strings using the '==' operator. You need to use the equals(..) method. When used on objects, the '==' operator evaluates true if both variables point to the same object - a test of identity. The equals(..) method tests if the value or contents of the two objects is the same.

    Always use equals(..) when comparing the values of two objects.

    It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
    F. Gauss
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Sep 2007
    Posts
    55

    Re: Using Split function, new to java

    Ah thanks that solved my problem.
    but I split by whitespace....and here's my problem, my program works GREAT when theirs just ONE space. is there a "special character" thats like....any amount of whitespace cuz right now im using split ("\\s") but if theirs more than one whitespace it messes it up

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Split function, new to java

    The split(..) method argument is a regular expression, so you can build up the pattern you want to match. In this case, you probably want 'one or more' whitespace characters, so if you append a '+' to the expression string, it should do what you want.

    The Java regex expressions are documented in the Pattern API JavaDoc.

    By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race...
    A.N. Whitehead
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Sep 2007
    Posts
    55

    Re: Using Split function, new to java

    so are u saying it'd be split("\\s+")?

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Split function, new to java

    It would take you a couple of minutes to try it and see, or to read the docs about it, but you'd rather wait an indefinite time for me to get back to you?

    The sole justification of teaching, of the school itself, is that the student comes out of it able to do something he could not do before. I say do and not know, because knowledge that doesn't lead to doing something new or doing something better is not knowledge at all...
    J. Barzun
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Sep 2007
    Posts
    55

    Re: Using Split function, new to java

    Well I mean I tried it and it seems to work but I want to make sure thats "how" it's supposed to be done and isn't just working....just by a fluke or something

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Using Split function, new to java

    Well, I don't really know what else to say - I told you what to do and how it works, I gave you a link to the docs so you could check it for yourself, you've tried it in code and it works... what else is there ?

    Teachers open the door, but you must enter by yourself...
    Chinese proverb
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured