• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick question: Java command

What the command name for looking at a string and taking 1 letter out to put into another string?

Such as a string with this value: bob
and I set it to pull from 0 to 1 or something so it set another string, say s1 to equal "b"

If you must know, I left the packet at school 😛
 
A character? String.charAt(int index) (Where first letter is index 0)
A substring? String.subString(int beginIndex) or String.subString(int beginIndex, int endIndex) depending on whether you want the rest of the string or have a definite endpoint.
 
Originally posted by: EyeMWing
A character? String.charAt(int index) (Where first letter is index 0)
A substring? String.subString(int beginIndex) or String.subString(int beginIndex, int endIndex) depending on whether you want the rest of the string or have a definite endpoint.

oh these have come in soo handy for our mock assembler

do google search for Java 1.4.2 API
 
Just put in a line to count how many letters are in the original string, then grab that number -1, say [4] and then make your new string with that letter string newLetter = stringOrig [4]
 
Originally posted by: AgaBooga
What the command name for looking at a string and taking 1 letter out to put into another string?

Such as a string with this value: bob
and I set it to pull from 0 to 1 or something so it set another string, say s1 to equal "b"

If you must know, I left the packet at school 😛

keep in mind, charAt() returns a char, if you want a 1 letter String result, you should use substring().
 
Originally posted by: FeathersMcGraw
Originally posted by: Ameesh
use stringbuilder if you are going to do it a lot cause strings are immutable

Is StringBuilder part of the MS Java library? 😉 I think you meant StringBuffer.

its what its called in .NET but they both are equivilant
 
import java.io.*;

public class cooneylikes
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); //Buffered Reader

System.out.print("Does Cooney Like: ");
String strInput = dataIn.readLine();

String strChar1 = strInput.charAt(0);
System.out.println(strChar1);
}
}

Thats my code so far, right now its just the basic setup, so none of the actual processing yet. But, I get an error for the line with charAt(0);

Here is the error:

found : char
required: java.lang.String
String strChar1 = strInput.charAt(0);
 
Originally posted by: AgaBooga
import java.io.*;

public class cooneylikes
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); //Buffered Reader

System.out.print("Does Cooney Like: ");
String strInput = dataIn.readLine();

String strChar1 = strInput.charAt(0);
System.out.println(strChar1);
}
}

Thats my code so far, right now its just the basic setup, so none of the actual processing yet. But, I get an error for the line with charAt(0);

Here is the error:

found : char
required: java.lang.String
String strChar1 = strInput.charAt(0);

if your class name has a verb in it your design is poor

 
Originally posted by: AgaBooga
import java.io.*;

public class cooneylikes
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); //Buffered Reader

System.out.print("Does Cooney Like: ");
String strInput = dataIn.readLine();

String strChar1 = strInput.charAt(0);
System.out.println(strChar1);
}
}

Thats my code so far, right now its just the basic setup, so none of the actual processing yet. But, I get an error for the line with charAt(0);

Here is the error:

found : char
required: java.lang.String
String strChar1 = strInput.charAt(0);

if your class name has a verb in it your design is poor

 
Originally posted by: AgaBooga
import java.io.*;

public class cooneylikes
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); //Buffered Reader

System.out.print("Does Cooney Like: ");
String strInput = dataIn.readLine();

String strChar1 = strInput.charAt(0);
System.out.println(strChar1);
}
}

Thats my code so far, right now its just the basic setup, so none of the actual processing yet. But, I get an error for the line with charAt(0);

Here is the error:

found : char
required: java.lang.String
String strChar1 = strInput.charAt(0);

if your class name has a verb in it your design is poor

 
Originally posted by: AgaBooga
found : char
required: java.lang.String
String strChar1 = strInput.charAt(0);

It's already been pointed out that charAt() returns char, not String. Use substring(index, index+1), or create a new String object using String.valueOf(char).
 
Well, like I said, right now its just the basics to get the bones of it working. I'll go through and do all that stuff once I get the basic structure created. Do you have any idea about the error though?
 
since java is soo awsome you can do something like:

System.out.println(strChar1); --> System.out.println(new Character(strChar1).toString()); //proper java way of doing that 😉
 
Originally posted by: AgaBooga
Well, like I said, right now its just the basics to get the bones of it working. I'll go through and do all that stuff once I get the basic structure created. Do you have any idea about the error though?

That IS the error. You're trying to initialize a String with a character. Try initializing it as follows:
String strChar1 = "" + strInput.charAt(0);

instead of
String strChar1 = strInput.charAt(0);

You need a literal string before you can add any characters to it. The "" serves as a literal string in this case, thus bypassing the error and your stupidity.
 
Originally posted by: Zugzwang152
Originally posted by: AgaBooga
What the command name for looking at a string and taking 1 letter out to put into another string?

Such as a string with this value: bob
and I set it to pull from 0 to 1 or something so it set another string, say s1 to equal "b"

If you must know, I left the packet at school 😛

keep in mind, charAt() returns a char, if you want a 1 letter String result, you should use substring().

I'd prefer to have it return a char right now, but how would I do that?

Is there another type for a single character or do I just use a string? How would I fix it using charAt?

What I basically need the program to do is take an inputted word and check to see if it has any two letters the same right next to each other, like the l's in fall, or the o's in books. If it does, output that the guy likes it otherwise that he doesn't like it.
 
Back
Top