13 Replies - 1426 Views - Last Post: 11 November 2009 - 05:06 PM Rate Topic: -----

#1 tasha9112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-May 09

String of integers to an Int

Post icon  Posted 09 November 2009 - 03:15 PM

hi there..

what i am tryin to do is write a recursive method which takes as input a string containing only the digits 0 to 9 and returning the integer represented by that string. like, "1234" becomes 1234.

what i have so far is this::

  public static boolean isNum(String str){

			if(str.length() < 1) return false;
			if(str.length() >= 1 && !Character.isDigit(str.charAt(0))) return false;
			//else if(str.length() == 2 && (!Character.isDigit(str.charAt(0)) || !Character.isDigit(str.charAt(1)))) return false;
			else if(str.length() > 2){
				if(!Character.isDigit(str.charAt(0)) || !Character.isDigit(str.charAt(str.length()-1))) return false;
				else return isNum(str.substring(1, str.length()-1));

			}

			return true;

			}
 	
public static void main (String[] args)
	{
		System.out.println("Is 123456 a num?- " + isNum("123456"));
		System.out.println("");
			   System.out.println("Is 123abc a num?- " + isNum("123abc"));
	}



by this you can see i am returning true and false, but i am am unsure on how to return the integers as in what i mentioned above.

Any help is appreciated thanks

Is This A Good Question/Topic? 0
  • +

Replies To: String of integers to an Int

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: String of integers to an Int

Posted 09 November 2009 - 04:26 PM

hi
ill give you some tips
to return the number represented as in, just "build it".
start working on the String from the last index of the String.
the last digit is digit * 10^0.
as we move left, we increse the power of 10.
so the second digit from the end would be digit * 10^1 and so on until we get to the first index.
the returned number would be int number, and you build it as (just a pseudocode)
number+= digit * 10^i
int i is the index of iteration, starts with 0, until str.length() -1)
String str is the number represented by the a string.

for example
1234 = 4 *10^0 + 3*10^1 + 2*10^2 + 1*10^3
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12211
  • View blog
  • Posts: 45,290
  • Joined: 27-December 08

Re: String of integers to an Int

Posted 09 November 2009 - 04:47 PM

Japanir is pointing you on the right track. Assuming that your String param is a number (something you may want to validate before hand), you may want to start at the length()-1 character and associate it with 10^0 and go from there. Here's the method header I'd use:

public int toNum(String s, int iteration){
	   ..if the length()-iteration-1 == 0 then what do you want to do?..
	   ..otherwise, I'd return a recursive call incrementing iteration by one PLUS (something else)..
}



You might also find it more efficient to use a similar setup for your isNum() method. As it is now, you are checking for more conditions than you have to.

Try implementing a solution for the toNum() method and feel free to post if you have any more questions. Remember to include your attempt. Good luck! :)
Was This Post Helpful? 0
  • +
  • -

#4 tasha9112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-May 09

Re: String of integers to an Int

Posted 10 November 2009 - 09:05 AM

hi again, i got to this point now::

	public static int stringToDigit (String s)
	{
		if (s.length() == 0) return 0;
		else
			 System.out.println (s.charAt(0) * 10^ s.length () - 1);
		return toInt(s.charAt(0) * 10^ s.length () - 1 + stringToDigit(s.substring(1)));
	}




but i am unsure of how to change the toInt to make it change the first character to an integer, and i am also getting some weird outputs for the power part of the function

i am a begier at java so i am unsure what i am doing wrong here and i cannot do anything too complex to get it to work
Was This Post Helpful? 0
  • +
  • -

#5 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: String of integers to an Int

Posted 10 November 2009 - 09:18 AM

If your program works, getting the number as a string, just parse the string to an int at the end.
Here's an example:
String s = "1234";
int x = Integer.parseInt(s);

now x = 1234, and is an int

also if you use this, it will throw NumberFormatException if the parameter(s) is NOT an int.
So if you write this in a try/catch block, you can do away with the isNum method.

This post has been edited by jimdandy75: 10 November 2009 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12211
  • View blog
  • Posts: 45,290
  • Joined: 27-December 08

Re: String of integers to an Int

Posted 10 November 2009 - 09:44 AM

You're off to a good start. :) However, just a couple of suggestions. One- '0' in char is not equal to the number 0; instead, it is equal to the number 60. The other numbers 1-9 follow the same suit. So '0'-'9' are equal to 60-69 respectively. And also, you can subtract characters like you can subtract numbers. Other than that, you could safely call your stringToDigit() method recursively.

@Jimandy75: I'm assuming Tasha knew about parseInt() and was trying to enhance her skills. However, under normal circumstances, I'd agree with you. Why reinvent the wheel? :)

This post has been edited by macosxnerd101: 10 November 2009 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 tasha9112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-May 09

Re: String of integers to an Int

Posted 10 November 2009 - 10:44 AM

hiya there, thanks for replying, i have not used parseint before. i have tried making another method where i had toInt in the code above and got this
	private static int toInt(int i) {
		String s <====here i want to call the string from the function stringToDigit, but i am unsure how
		// TODO Auto-generated method stub
		int x = Integer.parseInt(s);
	}



am i on the right track with adding another method to this? this just seems a little over comlicated for something so simple lol

Thank you


View Postjimdandy75, on 10 Nov, 2009 - 08:18 AM, said:

If your program works, getting the number as a string, just parse the string to an int at the end.
Here's an example:
String s = "1234";
int x = Integer.parseInt(s);

now x = 1234, and is an int

also if you use this, it will throw NumberFormatException if the parameter(s) is NOT an int.
So if you write this in a try/catch block, you can do away with the isNum method.

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12211
  • View blog
  • Posts: 45,290
  • Joined: 27-December 08

Re: String of integers to an Int

Posted 10 November 2009 - 11:06 AM

If you just want to use Integer.parseInt(), then you are way overcomplicating everything. Just use Jimandy's example, replacing "1234" with the String you want to convert.

View Postjimdandy75, on 10 Nov, 2009 - 12:18 PM, said:

String s = "1234";
int x = Integer.parseInt(s);

now x = 1234, and is an int

Was This Post Helpful? 1
  • +
  • -

#9 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: String of integers to an Int

Posted 10 November 2009 - 11:14 AM

Integer.parseInt() is a method that's in java.lang which is automatically imported.
Try this block of code and see if you understand what it's doing. You could also add
a return to the checkIt() method, if you want to do more work with the parsed int.
Hope this helps :)
public class IsNum{
	// make some strings 
	static String x = "1234";
	static String y = "Jenny 867-5309";
	static String z = "-23765";
	static String abc = "123$4";

	// in your main method you will call your check it method
	public static void main(String[] args){
		checkIt(x);  // pass your string into the method checkIt()
		checkIt(y);  // pass your string into the method checkIt()
		checkIt(z);  // pass your string into the method checkIt()
		checkIt(abc);  // pass your string into the method checkIt()
	}

	public static void checkIt(String input){
		try{
			int output = Integer.parseInt(input);
			System.out.println(input + " has been parsed to a valid integer!\n");
		}
		catch(NumberFormatException e){
			System.out.println(input + " is not a valid number and can't be parsed!\n");
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#10 tasha9112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-May 09

Re: String of integers to an Int

Posted 10 November 2009 - 11:18 AM

sorry i dont get where i am meant to put this :blink:
Was This Post Helpful? 0
  • +
  • -

#11 Atspulgs  Icon User is offline

  • D.I.C Addict

Reputation: 89
  • View blog
  • Posts: 507
  • Joined: 29-July 09

Re: String of integers to an Int

Posted 10 November 2009 - 11:19 AM

I have only a half a clue what are you trying to do but to turn String into int i used the following.
int number = Integer.valueOf(String str).intValue();


in practice:
int num = Integer.valueOf("1234").intValue();

This post has been edited by Atspulgs: 10 November 2009 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#12 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: String of integers to an Int

Posted 10 November 2009 - 11:22 AM

also when you get to building GUI's, Integer.parseInt() is an excellent method
to get strings from textbox's and parse to ints.

View Posttasha9112, on 10 Nov, 2009 - 10:18 AM, said:

sorry i dont get where i am meant to put this :blink:

Just run this by itself, if you understand it, then you should be able to use it in your program.
Was This Post Helpful? 0
  • +
  • -

#13 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: String of integers to an Int

Posted 10 November 2009 - 11:32 AM

View PostAtspulgs, on 10 Nov, 2009 - 10:19 AM, said:

I have only a half a clue what are you trying to do but to turn String into int i used the following.
int number = Integer.valueOf(String str).intValue();


in practice:
int num = Integer.valueOf("1234").intValue();


Same thing, as
int num = Integer.parseInt("1234");

Integer.parseInt() is just a cleaner version
Was This Post Helpful? 0
  • +
  • -

#14 tasha9112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 02-May 09

Re: String of integers to an Int

Posted 11 November 2009 - 05:06 PM

got it...
	public static int stringToDigit (String str)
	{
		if (str.length() == 0) return 0;
		else {
			int num = (int) str.charAt(0) - 48;
			
			if (num >= 0 && num <= 9) {
				return num * ((int) Math.pow(10, str.length()- 1)) + stringToDigit(str.substring(1));
			} else {
				return stringToDigit(str.substring(1));
			}
		}
	}



thanks everyone for your help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1