Uppercase Conversion












1















import java.util.Scanner;
import java.io.*;

public class UppercaseFileConverter {

public static void main(String args) throws FileNotFoundException{

Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file to be read: Here is the file converted into Uppercase.");
String fileName = input.nextLine();

File file = new File(fileName);
Scanner inputFile = new Scanner(file);

//validates that the file exists
if (!file.exists()) {
System.out.println("The file " + fileName + " does not exist or could not be opened.");
System.exit(0);
}

//if file exists then reads each line and prints the upper case
else {
while (inputFile.hasNext()) {

String line = inputFile.nextLine();

System.out.println(line.toUpperCase());
}
}

inputFile.close();
System.out.println("Files read, converted and then closed.");
}

}


When I run my code, my validation that checks whether the file entered exists or not does not run but instead terminates the program. Can I use a try/catch?










share|improve this question




















  • 2





    Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

    – lucumt
    Nov 28 '18 at 5:26













  • Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

    – iamrajshah
    Nov 28 '18 at 5:26











  • Can you share error logs or it System.exit is called.

    – Chetan Joshi
    Nov 28 '18 at 6:46
















1















import java.util.Scanner;
import java.io.*;

public class UppercaseFileConverter {

public static void main(String args) throws FileNotFoundException{

Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file to be read: Here is the file converted into Uppercase.");
String fileName = input.nextLine();

File file = new File(fileName);
Scanner inputFile = new Scanner(file);

//validates that the file exists
if (!file.exists()) {
System.out.println("The file " + fileName + " does not exist or could not be opened.");
System.exit(0);
}

//if file exists then reads each line and prints the upper case
else {
while (inputFile.hasNext()) {

String line = inputFile.nextLine();

System.out.println(line.toUpperCase());
}
}

inputFile.close();
System.out.println("Files read, converted and then closed.");
}

}


When I run my code, my validation that checks whether the file entered exists or not does not run but instead terminates the program. Can I use a try/catch?










share|improve this question




















  • 2





    Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

    – lucumt
    Nov 28 '18 at 5:26













  • Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

    – iamrajshah
    Nov 28 '18 at 5:26











  • Can you share error logs or it System.exit is called.

    – Chetan Joshi
    Nov 28 '18 at 6:46














1












1








1








import java.util.Scanner;
import java.io.*;

public class UppercaseFileConverter {

public static void main(String args) throws FileNotFoundException{

Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file to be read: Here is the file converted into Uppercase.");
String fileName = input.nextLine();

File file = new File(fileName);
Scanner inputFile = new Scanner(file);

//validates that the file exists
if (!file.exists()) {
System.out.println("The file " + fileName + " does not exist or could not be opened.");
System.exit(0);
}

//if file exists then reads each line and prints the upper case
else {
while (inputFile.hasNext()) {

String line = inputFile.nextLine();

System.out.println(line.toUpperCase());
}
}

inputFile.close();
System.out.println("Files read, converted and then closed.");
}

}


When I run my code, my validation that checks whether the file entered exists or not does not run but instead terminates the program. Can I use a try/catch?










share|improve this question
















import java.util.Scanner;
import java.io.*;

public class UppercaseFileConverter {

public static void main(String args) throws FileNotFoundException{

Scanner input = new Scanner(System.in);

System.out.println("Enter the name of the file to be read: Here is the file converted into Uppercase.");
String fileName = input.nextLine();

File file = new File(fileName);
Scanner inputFile = new Scanner(file);

//validates that the file exists
if (!file.exists()) {
System.out.println("The file " + fileName + " does not exist or could not be opened.");
System.exit(0);
}

//if file exists then reads each line and prints the upper case
else {
while (inputFile.hasNext()) {

String line = inputFile.nextLine();

System.out.println(line.toUpperCase());
}
}

inputFile.close();
System.out.println("Files read, converted and then closed.");
}

}


When I run my code, my validation that checks whether the file entered exists or not does not run but instead terminates the program. Can I use a try/catch?







java try-catch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 5:26









lucumt

5,73021023




5,73021023










asked Nov 28 '18 at 5:22









peopleGuy5peopleGuy5

133




133








  • 2





    Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

    – lucumt
    Nov 28 '18 at 5:26













  • Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

    – iamrajshah
    Nov 28 '18 at 5:26











  • Can you share error logs or it System.exit is called.

    – Chetan Joshi
    Nov 28 '18 at 6:46














  • 2





    Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

    – lucumt
    Nov 28 '18 at 5:26













  • Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

    – iamrajshah
    Nov 28 '18 at 5:26











  • Can you share error logs or it System.exit is called.

    – Chetan Joshi
    Nov 28 '18 at 6:46








2




2





Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

– lucumt
Nov 28 '18 at 5:26







Why do you use System.exit(0); in your code,if you do not want to do something ,just remove this line

– lucumt
Nov 28 '18 at 5:26















Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

– iamrajshah
Nov 28 '18 at 5:26





Yes there might be exception while checking for file, using try/catch will able to find the exact exception.

– iamrajshah
Nov 28 '18 at 5:26













Can you share error logs or it System.exit is called.

– Chetan Joshi
Nov 28 '18 at 6:46





Can you share error logs or it System.exit is called.

– Chetan Joshi
Nov 28 '18 at 6:46












1 Answer
1






active

oldest

votes


















0














You can do three things



1.Remove System.exit()



2.Add null Check before using file object



if (file!=null&&!file.exists()) {}


3.Add try catch block to handle possible exception in your case FileNotFoundException.






share|improve this answer
























  • I tried a try/catch block and found that the program executed like I wanted to. Thank you.

    – peopleGuy5
    Nov 28 '18 at 9:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512638%2fuppercase-conversion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can do three things



1.Remove System.exit()



2.Add null Check before using file object



if (file!=null&&!file.exists()) {}


3.Add try catch block to handle possible exception in your case FileNotFoundException.






share|improve this answer
























  • I tried a try/catch block and found that the program executed like I wanted to. Thank you.

    – peopleGuy5
    Nov 28 '18 at 9:33
















0














You can do three things



1.Remove System.exit()



2.Add null Check before using file object



if (file!=null&&!file.exists()) {}


3.Add try catch block to handle possible exception in your case FileNotFoundException.






share|improve this answer
























  • I tried a try/catch block and found that the program executed like I wanted to. Thank you.

    – peopleGuy5
    Nov 28 '18 at 9:33














0












0








0







You can do three things



1.Remove System.exit()



2.Add null Check before using file object



if (file!=null&&!file.exists()) {}


3.Add try catch block to handle possible exception in your case FileNotFoundException.






share|improve this answer













You can do three things



1.Remove System.exit()



2.Add null Check before using file object



if (file!=null&&!file.exists()) {}


3.Add try catch block to handle possible exception in your case FileNotFoundException.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 6:51









Chetan JoshiChetan Joshi

3,97021825




3,97021825













  • I tried a try/catch block and found that the program executed like I wanted to. Thank you.

    – peopleGuy5
    Nov 28 '18 at 9:33



















  • I tried a try/catch block and found that the program executed like I wanted to. Thank you.

    – peopleGuy5
    Nov 28 '18 at 9:33

















I tried a try/catch block and found that the program executed like I wanted to. Thank you.

– peopleGuy5
Nov 28 '18 at 9:33





I tried a try/catch block and found that the program executed like I wanted to. Thank you.

– peopleGuy5
Nov 28 '18 at 9:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512638%2fuppercase-conversion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)