String copy question

C

cyberninja

I have the copy part of this down, but i can't figure out how to copy just the int's from the string.



public class Main extends Object
{
public static void main(String []uuu)
{
String str1 = "",
str2,
str3 ="";

str2 = "1234Now is the time for All ";
str2 += "good men to come to the aid of tHEIR country123456789";
int k;


//Write the code to copy str2 to str1.
//You may not use any String class methods to perform the copy for you.

str1 = str2;




System.out.println(str1);


//Write the code to copy only the digits from str2 to str3.
//You may not use any string class method to perform the copy for you.


str3 = (int) str2;
System.out.println(str3);

}
}

This is where it gets odd, with the (int) it gives me an error.. was wondering what i would to do fix this and make it produce the output
 
C

Christophe Vanfleteren

cyberninja said:
I have the copy part of this down, but i can't figure out how to copy just
the int's from the string.



public class Main extends Object
{
public static void main(String []uuu)
{
String str1 = "",
str2,
str3 ="";

str2 = "1234Now is the time for All ";
str2 += "good men to come to the aid of tHEIR country123456789";
int k;


//Write the code to copy str2 to str1.
//You may not use any String class methods to perform the copy for
you.

str1 = str2;

This doesn't actually copy the String, you now just have two references to
the same String object.
System.out.println(str1);


//Write the code to copy only the digits from str2 to str3.
//You may not use any string class method to perform the copy for
you.


str3 = (int) str2;
System.out.println(str3);

}
}

This is where it gets odd, with the (int) it gives me an error.. was
wondering what i would to do fix this and make it produce the output

These kind of problems are better suited for c.l.j.help.

str3 = (int) str2;

That makes no sense, you're telling the compiler to cast a String to an int,
which is impossible, since a String is not an int.

As a solution to your problem, try something like looping over the indivdual
characters of the String, checking wether that Character is a digit, and if
it is a digit, append it to str3.

But you'll have to use some String methods though, like charAt.
 
B

Bryce (Work)

//Write the code to copy only the digits from str2 to str3.
//You may not use any string class method to perform the copy for you.


str3 = (int) str2;
System.out.println(str3);

Do you really think str3 = (int)str2; gets the digits from str2? If
so, then you are sadly mistaken.
 
R

Roedy Green

These kind of problems are better suited for c.l.j.help.

str3 = (int) str2;

That makes no sense, you're telling the compiler to cast a String to an int,
which is impossible, since a String is not an int.

If your string contains only digits, you can convert it to an int
using the method described at
http://mindprod.com/jgloss/converter.html


If it contains many ints, you first have to break you string up into
pieces. See http://mindprod.com/jgloss/regex.html using split, or
perhaps indexOf and substring, or perhaps using StringTokenizer.
 
M

M Smith

I would suggest, iterating through the string and comparing against ASCII
codes, that's working upon the presumption that you don't have any wacky
characters that may not be recognised, oh and you have to take into account
upper and lower.
so any thing that falls outside your numbers is not needed.

cyberninja said:
I have the copy part of this down, but i can't figure out how to copy just the int's from the string.



public class Main extends Object
{
public static void main(String []uuu)
{
String str1 = "",
str2,
str3 ="";

str2 = "1234Now is the time for All ";
str2 += "good men to come to the aid of tHEIR country123456789";
int k;


file://Write the code to copy str2 to str1.
file://You may not use any String class methods to perform the copy for you.

str1 = str2;




System.out.println(str1);


file://Write the code to copy only the digits from str2 to str3.
file://You may not use any string class method to perform the copy for you.


str3 = (int) str2;
System.out.println(str3);

}
}

This is where it gets odd, with the (int) it gives me an error.. was
wondering what i would to do fix this and make it produce the output
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top