9 Replies - 21719 Views - Last Post: 08 June 2006 - 08:26 PM Rate Topic: -----

#1 nizland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 06

how to return a specific digit in a number

Posted 06 June 2006 - 08:20 AM

Hi, there just wondering how can i return a specific digit in a given number, e.g. "56734", how can i return '5' in java? for example:
	public int getNum()
	{
		int i = 54321;
		String v = "" + i;
		return v.charAt(0);
	}


but in the example i got "53", can anyone see what i'm doing wrong here? and how can i fix it?
Thanks :D

Is This A Good Question/Topic? 0
  • +

Replies To: how to return a specific digit in a number

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: how to return a specific digit in a number

Posted 06 June 2006 - 08:38 AM

instead of String v = "" + i; as i believe it is trying to evalutate this before conversion, try String v = String.valueOf(i);. This should do a better conversion.
You could take it a step further (pending your use) and convert to a char array as well: char array[] = v.toCharArray(); then a for loop could be used, or any of the digits in your entered number :)
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: how to return a specific digit in a number

Posted 06 June 2006 - 01:34 PM

Wait a minute. Do you want to return a specific digit of an integer, or that of a String? If you want to do the first, it's simply math, and you don't need the String class - only the division (/) and modulus (%) operators, and maybe a loop.
If your input is in a String (then you could write your example as String v = "54321"; ) and the output must be an integer, then you could use Integer.parseInt(v.subString(index,index+1)) or simply use the String.parseInt to get the value of the whole number, and process it as in the previous case.
Was This Post Helpful? 0
  • +
  • -

#4 knownasilya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 11-January 06

Re: how to return a specific digit in a number

Posted 06 June 2006 - 02:01 PM

Edit:What he said ^!

blah.subString(2,3); 2 is starting and 3 is ending position.

This post has been edited by knownasilya: 06 June 2006 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 nizland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 06

Re: how to return a specific digit in a number

Posted 06 June 2006 - 05:13 PM

View Post1lacca, on 6 Jun, 2006 - 12:26 PM, said:

Wait a minute. Do you want to return a specific digit of an integer, or that of a String? If you want to do the first, it's simply math, and you don't need the String class - only the division (/) and modulus (%) operators, and maybe a loop.
If your input is in a String (then you could write your example as String v = "54321"; ) and the output must be an integer, then you could use Integer.parseInt(v.subString(index,index+1)) or simply use the String.parseInt to get the value of the whole number, and process it as in the previous case.


As I’m dealing with a class which containing the courses of the university, one of my constructor is input the Course code into it, e.g. ‘MATH 251’, and then I want to return the level of this course. i.e. the first digit of any given courseNumber.

	/**
	 * Constructor for objects of class Course.
	 */
	public Course(String dept, int number, int points)
	{
		this.dept = dept;
		this.courseNumber=courseNumber;
		pnts = points;
		int i=dept.length();
		courseNumber=number;
		if(!((i==4)||(courseNumber>=100&&courseNumber<=799)))
		{
			throw new IllegalArgumentException("Invalid course code");
		}
	}
.
.
.
	/**
	 * @return the level of the course, 
	 * i.e., 1, 2 etc for first year, second year, etc.
	 */
	public int getLevel()
	{
	return 0;
	}


So, if I was input ‘251’ in the constructor, there would return 2, as you said this could done with simple math operators, how it should be done to get it?
Thanks.

This post has been edited by nizland: 06 June 2006 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 nizland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 06

Re: how to return a specific digit in a number

Posted 06 June 2006 - 05:27 PM

Never mind, i got it now, and i convert to a string s, and return with Integer.parseInt(s.substring(0,1));, as "1lacca" said before.
Thanks for replying. :)

This post has been edited by nizland: 06 June 2006 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 Richard Goodwin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 09-May 06

Re: how to return a specific digit in a number

Posted 08 June 2006 - 12:48 PM

View Postnizland, on 6 Jun, 2006 - 04:19 PM, said:

Never mind, i got it now, and i convert to a string s, and return with Integer.parseInt(s.substring(0,1));, as "1lacca" said before.
Thanks for replying. :)

Im curious as to how the code would be written if you have an number stored as an integer and only need to return say the first digit.

Take Nizlands example above, what would the code look like for that?
Was This Post Helpful? 0
  • +
  • -

#8 nizland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-May 06

Re: how to return a specific digit in a number

Posted 08 June 2006 - 06:19 PM

View PostRichard Goodwin, on 8 Jun, 2006 - 11:40 AM, said:

View Postnizland, on 6 Jun, 2006 - 04:19 PM, said:

Never mind, i got it now, and i convert to a string s, and return with Integer.parseInt(s.substring(0,1));, as "1lacca" said before.
Thanks for replying. :)

Im curious as to how the code would be written if you have an number stored as an integer and only need to return say the first digit.

Take Nizlands example above, what would the code look like for that?



Hi there this is the code:

	public int getLevel()
	{
		int i = getNumber();
		String s = String.valueOf(i);
		return Integer.parseInt(s.substring(0,1));
	}
	
public int getNumber()
	{
		return courseNumber;
	}

This post has been edited by nizland: 08 June 2006 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#9 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: how to return a specific digit in a number

Posted 08 June 2006 - 07:32 PM

The method supplied does give the result, but if you do not wish to translate the number into a string, simply run a loop dividing by ten:
int digit; //set this to the integer value
while(digit >= 10){
	 digit = (int)digit/10;
}


this will remove the last digit each time, until there is only the first number remaining
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: how to return a specific digit in a number

Posted 08 June 2006 - 08:26 PM

If digit is an int, you dont need the cast (int) in the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1