a program using the terminal that determines the number of odd even and zero digits in an inputted value
Printable View
a program using the terminal that determines the number of odd even and zero digits in an inputted value
Code:import java.util.Scanner;
public class integervaluereader
{
public static void main (String[] args)
{
int place=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number..");
String input = scan.nextLine();
int length = input.length();
//char charAt(int index)
for(int f=0; f<length; f++)
{
char charat = input.charAt(f);
int charvalue = parseInt(charat);//error? this is where i need help.
}
}
}
What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
there isnt a parseInt(char) method
if there was i would just use that.
Then why are you trying to use it?
is there any method that does the same thing except for a string or int
Have you viewed the API ? You still have not understood what I've said - once again:
To which you replied,Quote:
What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
And yet you appear to already know this - YET you are trying to use it(see the irony there?). Where is parseInt a method of - what class does it belong to? - Answer this with your next post in this thread.Quote:
there isnt a parseInt(char) method, if there was I would just use that
do you even understand what im trying to do?
Carlos - I understand completely, based on what you've written. What you'll learn about me after being here for awhile is that I'm not going to hand you answers - if you want that, please look elsewhere. I'm here to assist you in learning the concepts - which you have failed to do after multiple attempts.
Answer my question above if you want a resolution to your problem.
ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?
can you tell me what im doing wrong?
That's the point of my previous post - there isn't. With that logic now known, how can you use a method that doesn't exist??
You still have not answered my question. What class does parseInt belong to? Answer - Integer class. Now - find the Integer class within the API. But a char is not what it takes - does it? What possible arguments does it take carlos? Read the API. It's there.
Indeed. You're not taking the initiative to read the API and further - you're not answering questions of those that are trying to help you - me.
And, I've already explained possible solutions - of which you should be able to infer, you're trying to use a method that doesn't exist!
The parseInt method takes a String, is there any way of NOT getting a char , but instead getting a String.. or converting a char to a string, am i on the right path?
Now I'll confess, I don't understand completely what you just said. I do know based on your original post that this was your problem:
actually, it's pretty obvious - you said this was your error.Code:int charvalue = parseInt(charat);//[B]error? this is where i need help.
[/B]
Finally, you read the API..Quote:
The parseInt method takes a String
NOT getting a char? You will not get a char, because the method returns a String. What's not clear here?Quote:
is there any way of NOT getting a char , but instead getting a String..
why do the evens get posted as zeros?Quote:
import java.util.Scanner;
public class integervaluereader
{
public static void main (String[] args)
{
int zero = 0;
int odd = 0;
int even = 0;
int place=0;
double remain=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number..");
String input = scan.nextLine();
int length = input.length();
// int n = Integer.parseInt(input);
//char charAt(int index)
for(int f=0; f<length; f++)
{
int charat = input.charAt(f);
remain = charat%2;
if (charat==0)
{
zero++;
}
else if (remain!=0)
{
odd++;
}
else if (remain==0)
{
even++;
}
}
System.out.println("zero: "+zero);
System.out.println("even: "+even);
System.out.println("odd: "+odd);
}
}
Input:444400005555Quote:
import java.util.Scanner;
public class integervaluereader
{
public static void main (String[] args)
{
int zero = 0;
int odd = 0;
int even = 0;
int place=0;
double remain=0;
boolean zb=false;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number..");
String input = scan.nextLine();
int length = input.length();
// int n = Integer.parseInt(input);
//char charAt(int index)
for(int f=0; f<length; f++)
{
zb=false;
int charat = input.charAt(f);
remain = charat%2;
if (remain!=0)
{
odd++;
}
else if ((charat == 0) && (remain==0))
{
zb = true;
zero++;
}
else if ((zb == false) &&(remain==0))
{
even++;
}
}
System.out.println("zero: "+zero);
System.out.println("even: "+even);
System.out.println("odd: "+odd);
}
}
Output:
Zero:0
Even:8
odd:4
whats wrong?
The end result looks like this with my formatting(since it wasn't properly formatted - use code tags, not quote tags).
Code:...
public class integervaluereader {
public static void main (String[] args) {
int zero = 0;
int odd = 0;
int even = 0;
double remain = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number..");
String input = scan.nextLine();
for (int i = 0; i < input.length(); i++) {
int ch = input.charAt(i); // returns the char value of the input,
// thus for the input 34025, you get
// char values of 51, 52, 48, 50, 53
// respectively
remain = ch % 2;
ch = Character.getNumericValue(ch);
if (ch == 0) {
zero++;
} else if (remain != 0) {
odd++;
} else if (remain == 0) {
even++;
}
}
System.out.println("zero: " + zero);
System.out.println("even: " + even);
System.out.println("odd: " + odd);
}
}