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
JavaBot
JavaBot3y ago
Hey, @dghf! Please remember to /close this post once your question has been answered!
dghf
dghfOP3y ago
Error: Could not find or load main class PersonligaUppgifter Caused by: java.lang.ClassNotFoundException: PersonligaUppgifter Process finished with exit code 1
JavaBot
JavaBot3y ago
// 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!");
}
}
// 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!");
}
}
dan1st
dan1st3y ago
How did you attempt to run your application?
dghf
dghfOP3y ago
How do you mean? How do I know what to add to the code to fix the errors?
dan1st
dan1st3y ago
how did you get the errors?
dghf
dghfOP3y ago
I just ran the program @dan1st | Daniel
dghf
dghfOP3y ago
No description
dghf
dghfOP3y ago
On IntelliJ IDEA Edu
dan1st
dan1st3y ago
is it in the src folder?
dghf
dghfOP3y ago
Yep
dghf
dghfOP3y ago
No description
dan1st
dan1st3y ago
you need to run Main, not Personligawhatever
dghf
dghfOP3y ago
No description
dghf
dghfOP3y ago
Like that?
dan1st
dan1st3y ago
yes
dghf
dghfOP3y ago
Actually I posted the wrong file Because Main is for a different file
dan1st
dan1st3y ago
run the file you want to run and it needs to be in the src folder
dghf
dghfOP3y ago
How do I see in what folder the file is in?
No description
dan1st
dan1st3y ago
on the left where is the file?
dghf
dghfOP3y ago
How do I open what'ss on the left
No description
dan1st
dan1st3y ago
click on the Projectbutton on the left and you should see the filed
dghf
dghfOP3y ago
Yep, but I can't find the folder for Personoliga…java
dan1st
dan1st3y ago
that's tze problem
dghf
dghfOP3y ago
No description
dghf
dghfOP3y ago
So I dragged a file for personligauppgifter.java to the screen
dan1st
dan1st3y ago
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
dghf
dghfOP3y ago
No description
dghf
dghfOP3y ago
Like this?
dan1st
dan1st3y ago
yes now open that file and run it
dghf
dghfOP3y ago
/format-code
/Users/…/eclipse-workspace/Ovning/src/PersonligaUppgifter.java:33:21
java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
/Users/…/eclipse-workspace/Ovning/src/PersonligaUppgifter.java:33:21
java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Btw, this is for an assignment
JavaBot
JavaBot3y ago
that doesn't work
that doesn't work
dan1st
dan1st3y ago
so the error says that a FileNotFoundException is thrown but not caught
dghf
dghfOP3y ago
/Users/…/eclipse-workspace/Ovning/src/PersonligaUppgifter.java:33:21
java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
/Users/…/eclipse-workspace/Ovning/src/PersonligaUppgifter.java:33:21
java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
dan1st
dan1st3y ago
Do you knoq how exceptions work?
dghf
dghfOP3y ago
Hmm, I read it in my book but I didn't grasp it that well.
dan1st
dan1st3y ago
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
dghf
dghfOP3y ago
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
dan1st
dan1st3y ago
yes
dan1st
dan1st3y ago
void a(){
try{
b();
}catch(Exception e){

}
}
void b() throws Exception{
throw new Exception();
}
void a(){
try{
b();
}catch(Exception e){

}
}
void b() throws Exception{
throw new Exception();
}
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
dghf
dghfOP3y ago
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?
dan1st
dan1st3y ago
yes, it continues in the {} of the catch block
dghf
dghfOP3y ago
Wow, thanks for simplifying whatever my book was attempting to explain in 1 page 😄
dan1st
dan1st3y ago
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

Did you find this page helpful?