Today is my first day in programming world i am trying to learn java so please excuse me for these sort of question
Deleting a stringbuffer object is throwing error?please help me to fix it my code is below
class Demo
{
public static void main(String[] args)
{
StringBuffer sb=new StringBuffer(); //default 16
sb.append("abc");
sb.append("xyz");
sb.append("123");
System.out.println("before delete():"+sb);
sb.delete(3,6);
System.out.println("after delete():"+sb);
sb.delete(99); //here error please help?
System.out.println("after delete():"+sb);
}
}
There is no method delete(int) defined in StringBuffer class.
It has to be delete(int, int). The other method which is offered by StringBuffer class is deleteCharAt(int)
Also, you string buffer object doesn't have anything at 99th index. Even if you use deleteCharAt(int) it is going to throw StringIndexOutOfBoundsException
I think there's no delete method with a single argument. You're calling a non-existing method.
StringBuffer
Related
BufferedInputStream.mark(int readlimit)
I read java doc but I don't understand when we use this parameter "readlimit"
in this code, I don't understand what's different between mark(1) or mark(100)
public static void main(String[] args) throws Exception {
String s="123456789ABCDEFGHIJKLMNOPQRSDVWXYZ";
byte byteArray[]=s.getBytes();
ByteArrayInputStream BArrayIS=new ByteArrayInputStream(byteArray);
BufferedInputStream BIS=new BufferedInputStream(BArrayIS);
BIS.mark(1);
System.out.println(BIS.read());
}
It has no effect because BufferedInputStream.mark method effects `` which is used in reset method that you don't use.
Repositions this stream to the position at the time the mark method
NO HELP RECIEVED..I FIXED IT MY SELF CANT REMOVE CODE. THANKS EVERYBODY WHO ACTUALLY HELPED NOT SPAM DOWN VOTES BECAUSE YOU DIDN'T LIKE THE QUESTION
}
public static void main(String[] args) {
new Login().setVisible(true);
}
}
If you are talking about these lines in readFile
while((a=breader.readLine()) != null)
{
System.out.print(a);
then the only way that that print can output "null" is if you read the characters "n" "u" "l" "l" from the input stream.
The problem is that in readFile(), if the file doesn't exist, then you are creating a new file and then calling WriteFile();
The 'file' variable from readFile() is not the same as the one from WriteFile(). They are both losing scope out of the functions they are declared in.
Also, you might want to look into your naming conventions: usually class names are the ones starting with an uppercase letter. Functions and variables should start with a lower case letter. So WriteFile() should be changed to writeFile(), to be consistent with readFile().
I am learning Java in this summer st college in US. I am new to Stringmethod.
I try to understand how to use Stringbuffer() method.
I tried to make ssl=statusBuffer in Else if section, but it causes error; how should I fix appropriate way?
Thank you so much.
public class test {
public static void main(String[] args){
String s= "123-45-6789";
String ssl;
int slength = (s.length());
if(slength ==11)
{
ssl = s;
}
else if(slength =9){
StringBuffer statusBuffer = new StringBuffer(s);
statusBuffer.insert(3,"-");
statusBuffer.insert(6,"-");
ssl=statusBuffer; //------ This part is causing error**
}
System.out.println(ssl);
}
}
ssl is a String. statusBuffer is a StringBuffer.
You need
ssl = statusBuffer.toString();
StringBuffer is not a string. You have to call statusBuffer.toString() to actually get the String from your StringBuffer.
In Java, Strings cannot be modified. If you create a String, it is always that String. If you modify a String, a new string is created. A StringBuffer is basically a modifiable String, which can be used for performance reasons when assembling big texts. They are also Thread safe. For more information, see the javadocs
I have two files:
Grader.getFileInfo("data\\studentSubmissionA.txt");
Grader.teacherFiles("data\\TeacherListA.txt");
Both contain a list of math problems, but the TeacherList is unsolved in order to check that the StudentSubmission was not altered from the original version.
studentSubmission is sent to the Grader class and the method currently looks like this:
public static void getFileInfo(String fileName)
throws FileNotFoundException {
Scanner in = new Scanner(new File(fileName))
while (in.hasNext()) {
String fileContent = in.nextLine();
}
and the TeacherFiles method looks like
public static void teacherFiles(String teacherFiles)
throws FileNotFoundException{
Scanner in = new Scanner(new File(teacherFiles));
while (in.hasNext()){
String teacherContent = in.nextLine();
String line = teacherContent.substring(0, teacherContent.indexOf('='));
}
I don't know how to get these methods to another method in order to compare them since they're coming from a file and I have to put something in the method signature to pass them and it doesn't work.
I tried putting them in one method, but that was a bust as well.
I don't know where to go from here.
And unfortunately, I can't use try/catches or arrays.
Is it possible to send the .substring(0 , .indexof('=')) through the methods?
Like line = teacherFiles(teacherContent.substring(0 , .indexof('='))); Is it possible to do this?
Think in more general terms. Observe that your methods called getFileInfo and teacherFiles, respectively are the very same except a few nuances. So why do not we think about finding the optimal way of merging the two functionalities and handling the nuances outside of them?
It is logical that you cannot use arrays as you need to know the number of elements of your array before you initialize it and your array would have already been initialized when you read the file. So using an array for this task is either an overkill (for example you allocate 1000 elements in your memory and you use only 10 elements) or insufficient (if you create an array of 10 elements, but you would need 1000). So, due to the fact that you do not know the number of rows in advance you need to use another data structure for your task.
So create the following method:
public static AbstractList<String> readFile(String filePath) throws FileNotFoundException, IOException {
Scanner s = new Scanner(new File(filePath));
AbstractList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
s.close();
return list;
}
Then use the method to read the student file and to read the teacher file. Store the results into two separate AbstractList<String> variables, then iterate through them and compare them as you like. Again, think in more general terms.
I am a bit lost here, i have been searching this for sometime.
I have a string $#897950%-1. Now i need to see if it contains a -1 in it. I tried
str.contains("-1") but it did not work. Can someone guide me to solve this ?
I tried it out as:
public static void main(String ar[]){
String str="$#897950%-1";
System.out.println(str.contains("-1"));
}
And See, what eclipse saw me, (It just work fine and gave me true as output):
Check whether you really have this string. May be debugger or output system replaces something.
I have the following code printing 'true'
public static void main(String[] args) {
String str = "$#897950%-1";
System.out.println(str.contains("-1"));
}