|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

02-03-2008, 11:41 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
how would i do this?
a program using the terminal that determines the number of odd even and zero digits in an inputted value
|
|

02-04-2008, 12:22 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
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.
}
}
}
Last edited by carlos123 : 02-04-2008 at 12:23 AM.
Reason: comment
|
|

02-04-2008, 12:42 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
|
What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 12:50 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
there isnt a parseInt(char) method
|
|

02-04-2008, 12:51 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
if there was i would just use that.
|
|

02-04-2008, 01:06 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
|
Then why are you trying to use it?
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 05:11 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
is there any method that does the same thing except for a string or int
|
|

02-04-2008, 06:16 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Have you viewed the API ? You still have not understood what I've said - once again:
What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
To which you replied,
there isnt a parseInt(char) method, if there was I would just use that
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 06:35 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
do you even understand what im trying to do?
|
|

02-04-2008, 06:40 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
|
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 06:42 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?
|
|

02-04-2008, 06:43 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
can you tell me what im doing wrong?
|
|

02-04-2008, 06:48 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by carlos123
ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
Last edited by CaptainMorgan : 02-04-2008 at 06:51 AM.
|
|

02-04-2008, 06:49 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by carlos123
can you tell me what im doing wrong?
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!
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
Last edited by CaptainMorgan : 02-04-2008 at 06:52 AM.
|
|

02-04-2008, 07:01 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
|
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?
|
|

02-04-2008, 07:06 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by carlos123
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:
int charvalue = parseInt(charat);//error? this is where i need help.
actually, it's pretty obvious - you said this was your error.
The parseInt method takes a String
Finally, you read the API..
is there any way of NOT getting a char , but instead getting a String..
NOT getting a char? You will not get a char, because the method returns a String. What's not clear here?
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 07:43 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
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);
}
}
why do the evens get posted as zeros?
|
|

02-04-2008, 07:55 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 38
|
|
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);
}
}
Input:444400005555
Output:
Zero:0
Even:8
odd:4
whats wrong?
Last edited by carlos123 : 02-04-2008 at 07:56 AM.
Reason: code
|
|

02-04-2008, 08:29 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by carlos123
why do the evens get posted as zeros?
You're not even getting the numeric values - remember charAt returns the char values. Add the bold line:
...
remain = charat % 2;
charat = Character.getNumericValue(charat);
...
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

02-04-2008, 08:32 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
The end result looks like this with my formatting(since it wasn't properly formatted - use code tags, not quote tags).
...
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);
}
}
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
Last edited by CaptainMorgan : 02-04-2008 at 08:52 AM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|