Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2008, 10:41 PM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
how would i do this?
a program using the terminal that determines the number of odd even and zero digits in an inputted value
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-03-2008, 11:22 PM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
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. } } }

Last edited by carlos123 : 02-03-2008 at 11:23 PM. Reason: comment
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-03-2008, 11:42 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-03-2008, 11:50 PM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
there isnt a parseInt(char) method
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-03-2008, 11:51 PM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
if there was i would just use that.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-04-2008, 12:06 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-04-2008, 04:11 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
is there any method that does the same thing except for a string or int
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-04-2008, 05:16 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Have you viewed the API ? You still have not understood what I've said - once again:
Quote:
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,
Quote:
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)
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-04-2008, 05:35 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
do you even understand what im trying to do?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-04-2008, 05:40 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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)
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 02-04-2008, 05:42 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
ive looked all over the API, i dont think there is a parseint(char) method. is there any other ways around this?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 02-04-2008, 05:43 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
can you tell me what im doing wrong?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 02-04-2008, 05:48 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by carlos123 View Post
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 05:51 AM.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 02-04-2008, 05:49 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by carlos123 View Post
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 05:52 AM.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 02-04-2008, 06:01 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 02-04-2008, 06:06 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by carlos123 View Post
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:
Code:
int charvalue = parseInt(charat);//error? this is where i need help.
actually, it's pretty obvious - you said this was your error.

Quote:
The parseInt method takes a String
Finally, you read the API..

Quote:
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)
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 02-04-2008, 06:43 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
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);



}
}
why do the evens get posted as zeros?
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 02-04-2008, 06:55 AM
Member
 
Join Date: Nov 2007
Posts: 38
carlos123 is on a distinguished road
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;
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 06:56 AM. Reason: code
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 02-04-2008, 07:29 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by carlos123 View Post
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:
Code:
... 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)
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 02-04-2008, 07:32 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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); } }
__________________

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 07:52 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 02:53 PM.