Hello, I am trying to complete a college course and everytime I go to put any line of code in to answer my chapter questions, replit says there is an error. I have input about 10 different lines of code now into replit, some of which have to be the correct answers to my chapter quiz questions. I cannot complete any coursework with replit malfuntioning like this. Any suggestions?
Hey, @BrennaSwinton welcome to the forums!
Can you please provide a link to the repl? This way it is easier for staff and members of the community to help you!
Also see this guide on how to share your code:
https://replit.com/@BrennaSwinton/Baby-Java#Main.java
Yes! Here is the line of code I typed in exactly from th website. I am not sure what is going on but I am getting errors left and right for code directly from my schools website.
Given the following lines of code, which of the following is the result when AA is entered when prompted? And this is the question I am attempting to answer with this line of code
Java requires the use of some boilerplate OOP (which you will probably learn about in future lessons), and your code seems to be missing it. Can you try the following and see if it works?
public class Main {
public static void main(String[] args) {
String inputRead = input.next();
int myNum = input.nextInt();
System.out.println("Your number is " + wholeNumber + ".");
}
}
Because of the way Java works, you have to wrap that code in a main call, like so:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Your code here
}
}
Otherwise, Java doesn’t know how to interpret that code.
Also, input
isn’t a built-in Java method AFAIK, so you might need more boilerplate than that.
Im not getting any lines of code to function, I feel almost as if the schools code moreso resembles python, I reached out to my school in case I can’t get it to function. I also have already deleted one java folder and tried a second and it still wont work It could also be that my knowledge base is too limited to try and troubleshoot considering I just began this class today
Yeah, that’s a good point. You will need to utilize Java’s Scanner feature for your code to work properly. Can you try the following?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number:");
int wholeNumber = input.nextInt();
System.out.println("Your number is " + wholeNumber + ".");
}
}
Don’t feel discouraged! Java is an extremely hard programming language compared to Python, and the only way you will get better is by practicing. Also, remember Google is your friend! If you ever encounter any errors you don’t know how to fix, try googling them or Ask here!
That line of code worked! Im not sure why all the other lines of code haven’t worked until now. I need to input AA, however, not a number
Sorry, I’m not a very good Java programmer . Can you evaluate on what AA is?
It’s just what the chapter question is asking me to input into my line of code from my schools website!
OOH, I think they are referring to the output of your program when the user inputs “AA”. Can you try to enter “AA” into your program and see what it outputs?
Edit: Sorry, I just realized that the code they provided suggests there is already a Scanner declared named input
. I edited my code above with these changes.
This explains why it resembles Python, it seems your school didn’t provide the full code. Have you learned about user input previously?
Apologies, just restarted my computer, here is another example of a line of code doing the same thing! I’m getting so frustrated grrrr
It also says my CPU and RAM are being maxed out on replit, but I dno’t have anything else running, I am about to just start guessing at this point!
Hi @BrennaSwinton,
Remember that Java requires the boilerplate OOP code, otherwise your program will not function. You also need to create a Scanner, or you won’t be able to collect input. Can you try the following and see if it works?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number:");
int wholeNumber = input.nextInt();
System.out.println("Your number is " + wholeNumber + ".");
}
}
The following code should collect a number and print "Your number is ", along with the number that the user enters.
Your school seems to be asking what the output is when you are to type “AA”, instead of a number with the above code. Can you try that and see what it outputs as a response?
That worked perfectly, I feel as though my school is not providing the full code needed to answer their questions. It has not gone over scanners, nor does it include it in asking us to type out the code! I have reached out to the school to see what they recommend, bu thank you for your assistance!
No problem! If you found mine or any other replies helpful, you can mark them as the solution so other people with the same problem can easily find the solution as well, and the topic can eventually close.