6 Replies - 716 Views - Last Post: 27 October 2012 - 06:52 AM Rate Topic: -----

#1 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Returning Last Token in String

Posted 27 October 2012 - 05:00 AM

Hey guys I was wondering if anyone could help me out.
I would like to know how I can get the last token in the string to be returned
as model.The carName reads in a whole line ie Ferrari Enzo 350
And I want to use the while loop to return the last last token as model
how can I achieve this?
public String getModel() {
		Scanner tokenizerModel = new Scanner(carName);
		String model = null;

		// Last token in Loop is model
		while (tokenizerModel.hasNext()) {
			
		}
		return model;

	}


Is This A Good Question/Topic? 0
  • +

Replies To: Returning Last Token in String

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Returning Last Token in String

Posted 27 October 2012 - 05:05 AM

So do you want Enzo or 350 to be returned?

It will probably be easier to use the String.split and return the last index in the returned array rather than using a Scanner for this.
Was This Post Helpful? 0
  • +
  • -

#3 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Returning Last Token in String

Posted 27 October 2012 - 05:08 AM

View PostRyano121, on 27 October 2012 - 05:05 AM, said:

So do you want Enzo or 350 to be returned?

It will probably be easier to use the String.split and return the last index in the returned array rather than using a Scanner for this.

I just want to return the last token so 350 in this case.I am using the scanner so I can read the last token as I am then
using it in a compare method.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,083
  • Joined: 05-April 11

Re: Returning Last Token in String

Posted 27 October 2012 - 05:08 AM

There are many ways to achieve this

You could use split. If you have a car class you need to initialize with the information, then the split method is a good choice
String[] tokens = carName.split(" ");
tokens[0]; //Ferrari
tokens[1]; //Enzo
tokens[2]; //350



If you only want the model, then you can substring from the last space
String model = carName.substring(carName.lastIndexOf(" ") + 1);


Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Returning Last Token in String

Posted 27 October 2012 - 05:10 AM

In that case you can simply do something like

String[] tokens = carName.split(" ");
return tokens[tokens.length - 1];


much easier and shorter than using Scanner :)
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1956
  • View blog
  • Posts: 4,058
  • Joined: 11-December 07

Re: Returning Last Token in String

Posted 27 October 2012 - 06:39 AM

Do you have a good reason not to spend 5 minutes making a car class?
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1956
  • View blog
  • Posts: 4,058
  • Joined: 11-December 07

Re: Returning Last Token in String

Posted 27 October 2012 - 06:52 AM

Actually, I should qualify this. I don't know what you are making. If you are doing an exercise in string manipulation then what you are doing is fair enough. Also, if you are writing a program to work something out then it can be fair enough too because you just want the answer and the code will get binned soon.

But as soon as you incorporate that sort of thing into a bigger program, you end up making rabbit warrens of logic and jumping through increasingly complex hoops to do the simplest of things.

public class Car {

  private String make;
  private String model;
  private int number;
  // Don't know enough about cars to give this a better name.

  public Car(String make, String model, String number) {
    this.make = make;
    this.model = model;
    this.number = number;
  }

  public static Car makeFromString(String str) {
    Scanner sc = new Scanner(str);
    return new Car(sc.next(), sc.next(), sc.nextInt());
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public int getNumber() {
    return number;
  }

}


Representing multiple fields as tokens in a String is a recipe for disaster on object orientated code. Represent them as objects and your code will become much simpler. The next trick to learn is to place methods in your classes so that you don't need as many getters and setters. That's when the objects start interacting with each other and your code melts away into simplicity.

This post has been edited by cfoley: 27 October 2012 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1