Help
// PersonligaUppgifter.java
// ett program som hanterar personliga uppgifter
/**
Anv�ndaren matar in sina personliga uppgifter. Dessa uppgifter sparas
sedan i en fil.
**/
class PersonligaUppgifter {
public static void main(String[] args)
// throws Exception // (1)
{
System.out.println("PERSONLIGA UPPGIFTER");
System.out.println();
// inmatningsverktyg
java.util.Scanner in = new java.util.Scanner(System.in);
// mata in personliga uppgifter
System.out.print("Fodelsear: ");
int ar = in.nextInt();
// in.nextLine(); // (2)
System.out.print("Ditt fornamn och efternamn: ");
String namn = in.nextLine();
System.out.println();
// spara uppgifter i en fil
java.io.PrintWriter fout =
new java.io.PrintWriter("persUpp.txt");
fout.println(namn + ": " + ar);
fout.flush();
// ett meddelande
System.out.println("Oppna filen persUpp.txt!");
}
}
45 Replies
Hey, @dghf!
Please remember to
/close
this post once your question has been answered!Error: Could not find or load main class PersonligaUppgifter
Caused by: java.lang.ClassNotFoundException: PersonligaUppgifter
Process finished with exit code 1
How did you attempt to run your application?
How do you mean?
How do I know what to add to the code to fix the errors?
how did you get the errors?
I just ran the program
@dan1st | Daniel

On IntelliJ IDEA Edu
is it in the src folder?
Yep

you need to run Main, not Personligawhatever

Like that?
yes
Actually I posted the wrong file
Because Main is for a different file
run the file you want to run
and it needs to be in the src folder
How do I see in what folder the file is in?

on the left
where is the file?
How do I open what'ss on the left

click on the
Project
button
on the left
and you should see the filedYep, but I can't find the folder for Personoliga…java
that's tze problem

So I dragged a file for personligauppgifter.java to the screen
you need to move that fike to the src folder
that doesn't work
you need to drag it to the src folder
not into the editor

Like this?
yes
now open that file
and run it
/format-code
Btw, this is for an assignment
so the error says that a FileNotFoundException is thrown but not caught
Do you knoq how exceptions work?
Hmm, I read it in my book but I didn't grasp it that well.
it is possible to create and throw exceptions
when an exception is thrown, the execution stops
and it goes up the stack until it reaches a catch block where that exception is caught
can you give an example please, specifically for
when an exception is thrown, the execution stops … goes up the stack until it reaches a catch block where that exception is caught
yes
in this example, a calls b
b throws an exception
when that happens, the program stops
it goes back to a
until it reaches a catch block
since there is a catch block, it continues there
so when we write throw Exception, the program is going back and making an exception for whatever that is causing the error inbetween the { } after catch … and continuing with the program?
yes, it continues in the {} of the catch block
Wow, thanks for simplifying whatever my book was attempting to explain in 1 page 😄
when an exception is theown, it goes back until it finds a catch block
and there is a difference between checked and unchecked excepttions
checked exceptions require you to have a catch block
if a method throws a checked exception without being in a try-catch block, it needs to have a
throws
declaration
this indicates to others that the method might throw exceptions
and other methods calling it need to have a catch block
try catch means run something (in the try block) and if an exception occurs, continue in the catch block