im trying to convert the int input into a string so that i can find the length of it and use the substring method. how may i go about this?

import java.util.Scanner;

public class OddEvenZero
{
	public static void main(String[] args)
	{
		int input = 0;
		int len = 0;
		int currentValue = 0;
		int numOdd = 0;
		int numZero = 0;
		int numEven = 0;
		int offset = 0;
		int end = 0;
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please enter an integer.");
		input = scan.nextInt();
		// need to convert this int (input) into a string here!!
		
		
		
		len = input.length();
		end = 1;
		currentValue = input.substring(offset, end);
		
		while (end <= len)
		{
			if (currentValue == 0)
			{
				numZero = numZero + 1;
			}
			
			else if (currentValue == 1 || currentValue == 3 || currentValue == 5 || currentValue == 7 || currentValue == 9)
			{
				numOdd = numOdd + 1;
			}
			else
			{
				numEven = numEven + 1;
			}
			
			offset++;
			end++;
		}
		System.out.println("Number of zeros: " + numZero);
		System.out.println("Number of even integers: " + numEven);
		System.out.println("Number of odd integers: " + numOdd);
		
	}
}

Recommended Answers

All 12 Replies

you don't have any problem to cast int to string
int myInteger = 25
String myString = myInteger;

what's your problem?!

im getting the compile error:

D:\ChThree\OddEvenZero.java:27: Obj is already defined in main(java.lang.String[])
String Obj = input;

when i use this code:

String Obj = "";
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Please enter an integer.");
		input = scan.nextInt();
		
		String Obj = input;
		
		len = Obj.length();

you don't have any problem to cast int to string
int myInteger = 25
String myString = myInteger;

what's your problem?!

This is not valid at all.

To the OP, use String.valueOf(int value).

so, that's JAVA!
but when I was using it, I think I did something before!!!!!!!

commented: Please do try answer posts, but do not leave pointless comments -1

so, that's JAVA!
but when I was using it, I think I did something before!!!!!!!

Well, yes, this is the Java forum, so posting something that is not valid Java code is less than useful.

Honestly, I cannot even fathom what you are trying to say in the second line of that comment...

I really do not understand why some people even reply. It seems like they want to do anything BUT be helpful.

If you meant me, I DON'T LIKE IDIOT COMMENTS!!! I wanted just to help!

int myInt = 5;
String myStr;
myStr = "" + myInt;
int myInt = 5;
String myStr;
myStr = "" + myInt;

Yes, this does work, but it is not an efficient way to go about it. As I posted above

int myInt=5;
String strValue = String.valueOf(myInt);

is more appropriate.

and this?

int myInt = 5;
String myStr;
myStr = myInt.tostring();

and this?

int myInt = 5;
String myStr;
myStr = myInt.tostring();

Is invalid. Primitives like int are not objects and do not have methods.

thanks for this information :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.