Results 1 to 20 of 20
Thread: how would i do this?
- 02-03-2008, 09:41 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
- 02-03-2008, 10:22 PM #2
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Java 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 10:23 PM. Reason: comment
- 02-03-2008, 10:42 PM #3
What class is the method parseInt(char) apart of? Once you find that out, you should be able to write the correct code.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-03-2008, 10:50 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
there isnt a parseInt(char) method
- 02-03-2008, 10:51 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
if there was i would just use that.
- 02-03-2008, 11:06 PM #6
Then why are you trying to use it?
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 03:11 AM #7
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
is there any method that does the same thing except for a string or int
- 02-04-2008, 04:16 AM #8
Have you viewed the API ? You still have not understood what I've said - once again:
To which you replied,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.there isnt a parseInt(char) method, if there was I would just use thatVote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 04:35 AM #9
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
do you even understand what im trying to do?
- 02-04-2008, 04:40 AM #10
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.Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 04:42 AM #11
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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, 04:43 AM #12
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
can you tell me what im doing wrong?
- 02-04-2008, 04:48 AM #13
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.Last edited by CaptainMorgan; 02-04-2008 at 04:51 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 04:49 AM #14
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!Last edited by CaptainMorgan; 02-04-2008 at 04:52 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 05:01 AM #15
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
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, 05:06 AM #16
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.Java Code:int charvalue = parseInt(charat);//[B]error? this is where i need help. [/B]
Finally, you read the API..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?is there any way of NOT getting a char , but instead getting a String..Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 05:43 AM #17
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
why do the evens get posted as zeros?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);
}
}
- 02-04-2008, 05:55 AM #18
Member
- Join Date
- Nov 2007
- Posts
- 38
- Rep Power
- 0
Input:444400005555import 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?Last edited by carlos123; 02-04-2008 at 05:56 AM. Reason: code
- 02-04-2008, 06:29 AM #19
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 06:32 AM #20
The end result looks like this with my formatting(since it wasn't properly formatted - use code tags, not quote tags).
Java 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); } }Last edited by CaptainMorgan; 02-04-2008 at 06:52 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks