9 Replies - 676 Views - Last Post: 02 October 2012 - 02:18 PM Rate Topic: -----

#1 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

java compare string to string

Posted 02 October 2012 - 06:59 AM

The task is to import file and find the prepositions (about, in, the...) counting them. Java does not compare strings well. How can I go around it? Tried to change it to compare char array. That helped with prep words but not sure how to put file info into array that is not string.
Is This A Good Question/Topic? 0
  • +

Replies To: java compare string to string

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,447
  • Joined: 27-December 08

Re: java compare string to string

Posted 02 October 2012 - 07:08 AM

The String class has a compareTo() method, as well as a compareToIgnoreCase() method.

http://docs.oracle.c...ang/String.html
Was This Post Helpful? 0
  • +
  • -

#3 M4trixSh4d0w  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 07-May 10

Re: java compare string to string

Posted 02 October 2012 - 07:09 AM

Have you tried:
str.equals(str2)

This post has been edited by M4trixSh4d0w: 02 October 2012 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java compare string to string

Posted 02 October 2012 - 07:11 AM

if(str.equals(str2)){
   System.out.println("Euston, we have a match!");
}else{
   System.out.println("Dang, no match!");
}


Whoops, thought you made the post above me. For your specific task, probably best to use compareTo()

This post has been edited by nick2price: 02 October 2012 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java compare string to string

Posted 02 October 2012 - 07:40 AM

View Postcodescout, on 02 October 2012 - 09:59 AM, said:

Java does not compare strings well.

Wher did you get this info ? Based on what ? Is this a new urban legend ?
I'll bet you are improperly using the == operator :)

May be you can benefit from Dogstopper's tutorial
http://www.dreaminco...he-string-pool/

This post has been edited by pbl: 02 October 2012 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: java compare string to string

Posted 02 October 2012 - 11:49 AM

Thanks for the info and links. Will investigate. Could you look to see why it's not reading the file in? Have tried to use FileWriter without success and have imported all corresponding java.io's.

[Scanner scan = null;
PrintWriter output = null;
FileReader inputStream = new FileReader("C:\\Users\\file.txt");

String[] keyWords = {"at", "about", "until", "of", "in"};
int counters = 0;

try {
scan = new Scanner(inputStream);
output = new PrintWriter("C:\\Users\\file2.txt");

while (scan.hasNext()) {
String nextLine = scan.nextLine();]
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: java compare string to string

Posted 02 October 2012 - 11:52 AM

Quote

String nextLine = scan.nextLine();

Doesn't really go with the preceding line. It should probably be
String nextLine = scan.next(); 

Otherwise that's ok in principle
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java compare string to string

Posted 02 October 2012 - 01:16 PM

And the reason its not reading your file in is because it looks like the url is wrong. Give it a relative path from your class file. If you do use an absolute path, shouldnt it be

Quote

C:\Users\file.txt

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: java compare string to string

Posted 02 October 2012 - 02:01 PM

Quote

Give it a relative path from your class file.


That's not a good idea. For one thing, relative paths are not relative to he class file, they're relative to the current directory. And that is not fixed necessarily.

It would be better to refrain from hardcoding anything. The absolute path can be passed as a parameter to the application
Was This Post Helpful? 2
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: java compare string to string

Posted 02 October 2012 - 02:18 PM

Whoops, my mistake. I generally compile my class files to the working directory which is where i got confused. The best option would be to use an absolute path passed to the application as a parameter like you said, I dont think this is often done by beginners though. Instead, it is common to hardcode the absolute path, which is something I dont personally like because if you have numerous calls each containing a path, it can be a pain having to change them all if you move server or something.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1