I'm always having this error but error and I don't know how to fix it. Ok I will explain you.
I'm starting to learn code right now and when I write anything with dots(like System.out.printIn()) it gives me this error:cant find symbol. And it points the dot before printIn.
But its not on all dots, last code i write was:
public class Pryt {
public static void main(String[] args) {
String aString = "mynameisigna";
System.out.println(aString);
String reverse = new StringBuffer(aString).reverse().toString();
System.out.println(reverse);
System.out.println(**aString.lenght**);
}
}
And in this case it gives me the error on
System.out.println(aString.lenght);
The only solution I can take is to copy a example code from internet and I don't find this solution useful.
check your spelling - it's length, not lenght
it's a method, therefore aString.length()
Related
We were told to do a program on stings and I wasn't able to attend class because I was sick. I am asking for your help on this task that was given to us.
Create a java program that will ask the user to input two Strings. Compare the two strings and display the letters that are found on the first string but are not found on the second string.
Here is what I have at the moment https://pastebin.com/7a4dHecR
I really have no Idea what to do so any help would be appreciated!
https://pastebin.com/7a4dHecR
import java.util.*;
public class filename{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.print("Input first string: ");
String one=sc.next();
System.out.println();
System.out.print("Input second string: ");
String two=sc.next();
}
}
There are many ways to do this. I'm going to give you some parts you can put together. They are not the shortest or simplest way to solve this particular problem, but they will be useful for other small programs you write.
Here are some hints:
First, figure out how to step through your code with a debugger.
Second, figure out how to find the Javadoc for Java library classes and their methods.
You need to do something for each character in a string. Use a for loop for that:
for (int i = 0; i < one.length(); i++) {
// your code here
}
You need to get a particular character of a String.
String c = one.substring(i, i+1);
Read the Javadoc for String.substring to understand what the i and i+1 parameters do.
Now you need to find a way to check whether a String contains another String. Look at the Javadoc for the String class.
Then you can put all this together.
You could try the following:
String diff: StringUtils.difference(one, two);
System.out.println(diff);
Just skip to the question if you don't care about examples/backstories
Story behind this
I'm currently trying to debug a piece of code so that I can fix any problems so I won't have to rewrite the whole thing later. Unfortunately, it prints out an eyesore so I'm trying to clean it up
Examples
These examples already include the fix I tried, but I marked it out. Also, please note they're all canon with each other
The Main class (the one that prints out the debug info)
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(Foo.toString).useDelimiter("\\[|\\]|,]"); //I'm using the delimiters "[", "]", and ",", because they're used for the debug program to separate Objects/Arrays
//My fix starts here
String debugOutput = "Output: "
do{
try {
foo = s.next();
debugOutput = debugOutput + foo + "\n";
} catch (NoSuchElementException ignore){
bar = false;
}
} while (bar);
//My fix ends here
System.out.println(debugOutput); //Without a fix, the debugOutput would be debugInfo
}
}
The Foo class that I'm debugging
class Foo{ //Class I'm trying to debug
//The code I'm busy debugging (for a list of classes, see the output as I don't feel like typing it all out
#Override
public String toString() {return ToStringBuilder.reflectionToString(this);}
}
The output I'm getting without my "fix"
ExampleClass#123456[ExampleArray1=[ExampleString1="Example String 1",ExampleString2="Example String 2"],ExampleObject1#234567=[ExampleString3="Example String 3",ExampleString4="Example String 4"],ExampleString5="Example String 5"]
The output I'm looking for (doesn't have to be exactly the same)
ExampleClass#123456
ExampleArray1=
ExampleObject2="Example String1"
ExampleObject3="Example String2"
ExampleObject1#234567=
ExampleString3="Example String 3"
ExampleString4="Example String 4"
ExampleString5="Example String 5"
External libraries that I'm using
org.apache.commons.lang3.builder.ToStringBuilder
My question
Is there a way to find out what delimiter has last been used?
It doesn't have to be Scanner, and since this won't be in the final code, it doesn't matter how messy the solution is.
I realize this is a popular question to ask, but all the questions were looking for a more closed answer. I'm fine with changing a lot of what I've done so far.
class address
{
String address;
String newaddr = address.trim();
final int ziplength =4;
String input;
Scanner in = new Scanner(System.in);
String temp = in.next();
String zipcode = input.substring(input.length()-ziplength);
try **//illegal start type error**
{
Integer.parseInt(zipcode);
System.out.println("PO is: "+zipcode);
}
catch( Exception e) **//illegal start type error**
{
System.err.println("Last 4 chars are not a number.");
}
}
This code segment extract the last four characters from a string, and see if they are post code.
I have commented the point reporting "illegal start type error" in NetBeans.
I wonder, if I cannot use try-catch when creating a class? Or, do this class miss something?
I tried searching stackoverflow. But I am still confusing. Here are some links.
Java illegal start of type
Java error: illegal start of expression
java: Why does the program give "illegal start of type" error?
Java does not allow you to simply put statements in the body of a class. You always need an enclosing "block" around those statements.
In other words: the easiest way to a first working example would be to add a main method to your class and to move your code in there. Meaning a method with signature public static void main(String[] args)
Beside that: don't "wait" until several errors come together. Start with an empty class. Enter one new construct in there. Save; run the compiler. Go for the next "element" that you need.
For a beginner, your strategy (lets write 10, 20 lines of code; and then lets hope it works) will not work at all. You are wasting your time (and ours) by doing it like that. You see, this is so basic stuff that you should not turn to other people to explain them to you. You should start small and figure all these things yourself. Because that is the essence of learning programming.
class address
{
String address;
String newaddr = address.trim();
final int ziplength =4;
String input;
Scanner in = new Scanner(System.in);
String temp = in.next();
String zipcode = input.substring(input.length()-ziplength);
public address() //this is the only thing I add, but it eliminate "illegal start type error"
{
try
{
Integer.parseInt(zipcode);
System.out.println("PO is: "+zipcode);
}
catch( Exception e)
{
System.err.println("Last 4 chars are not a number.");
}
}
}
Special thank you for #Jägermeister . He gives me valuable hint.
Since I am a beginner, I am thinking a better way to improve my skills. I will try more.
I am trying to do this stuff. If a user enters "C:\Windows\system32\foo.txt" then the program will convert it to "C:\\Windows\\system32\\foo.txt". A front slash needs to be added to every other preceding slash. Here's what I have coded till now (only the section relevant):
import javax.swing.*;
public class test {
public static void main(String[] args){
String path = JOptionPane.showInputDialog(null, "Enter the File path", "Word counter", JOptionPane.INFORMATION_MESSAGE);
for (int z=0;z<=path.length()-1;z++)
{
if (path.charAt(z) == '\\')
{
path.charAt(z) = "\\\\";
}
}
System.out.println(path); // For knowing what's going on
}
}
Unfortunately it's not compiling, and I don't have a clue of what to do. Any possible help welcomed. Thank you!
You are trying modify a String. Remember strings are immutable.
you can try something like
path.replace(oldChar, newChar) if you want to replace some chars.
This: path.charAt(z) cannot be on the left side of an assignment statement. Instead concatenate your String or use a StringBuilder.
Or just use String's replace(...) method.
I am writing a program that counts song lyrics. Right now I have it programmed to delete certain characters using line.replace, for example:
String computerComma=",";
String computerPeriod=".";
String nothing="";
line=line.replace(computerComma,nothing);
line=line.replace(computerPeriod,nothing);
and this works totally fine. However, when I try
String computerExclamation="!";
line=line.replace(computerExclamation,nothing);
it messes up my entire program and many of my word counters. Does anybody know the reason behind this?
Thanks!
No. Works fine.
public static void main(String[] args) {
String computerExclamation="!";
String line = "i am a String !!.";
line=line.replace(computerExclamation,"");
System.out.println(line); //i am a String .
}
Error lies some where else.
You can see here.