java.util.scanner

Deleting a certain character from a text file in java

牧云@^-^@ 提交于 2019-12-13 05:32:56
问题 So I've recently received a large text file that I need to read unfortunately its formatted terribly as there are next line characters everywhere making it extremely difficult to read. So I've been trying to think of a way to sort through the file and delete each nextLine using a java program. For example if we had this text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus interdum condimentum. Proin viverra justo vel imperdiet sagittis, purus sapien sagittis mi,

while (scanner.hasNext()) loop not working twice? Java

拈花ヽ惹草 提交于 2019-12-13 05:27:58
问题 I have made a program which is like a vending machine! My code is similar to: public static void main (String [] args) { Scanner sc = new Scanner (System.in); while(sc.hasNext()) { String string = sc.next(); sum = generateSum(sum) ..... } } public static int generateSum(int sum) { Scanner sc = new Scanner (System.in); while (sc.hasNext()) { .... } return sum; } Sorry for simplifying my code, but the normal one is very long! However, the problem is that I use while (sc.hasNext()) loop twice.

Scanner will take user input but will not find the file

北慕城南 提交于 2019-12-13 04:50:10
问题 This is a program to read a file and print out the file with some of the text edited. The code will compile the issue is that it will read the users input but will say file is not found when the file is there. I feel like I am missing something. I am brand new at this so go easy on me. 回答1: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class MainTest { public static void main(String args[]) { // if (args[0] != null) readFile(); } public static

Reading from file double value using Scanner - InputMismatchException?

走远了吗. 提交于 2019-12-13 04:31:16
问题 I tried read from file double values and using Scanner with this aim. It throws InputMismatchException : "input.txt" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextDouble(Scanner.java:2456) And I can't understand why this happen? Code: public class Largest { public static void main(String[] args) throws FileNotFoundException { String filename = "input.txt"; Scanner in = new Scanner(filename

Reading CSV file line by line and parsing it

妖精的绣舞 提交于 2019-12-13 03:23:13
问题 I have a CSV file that I need to read line by line with the help of a Scanner and store only country names into an array of strings. Here is my CSV file: World Development Indicators Number of countries,4 Country Name,2005,2006,2007 Bangladesh,6.28776238,13.20573922,23.46762823 "Bahamas,The",69.21279415,75.37855087,109.340767 Brazil,46.31418452,53.11025849,63.67475185 Germany,94.55486999,102.2828888,115.1403608 This is what I have so far: public String[] getCountryNames() throws IOException,

WebScanner Java Applet App Not working in chrome , NPAPI deprecated any solutions?

半城伤御伤魂 提交于 2019-12-13 03:06:52
问题 Java Applet is not working in chrome, As NP-API is been depreciated is there any alternative solution, to load applet in browser and scanner is invoked to scan the file. Advance Thanks, any help is appreciated. Scanner Applet application 回答1: What's the chrome version?? Copy and paste "chrome://flags/#enable-npapi" into your Chrome browser and hit enter Click the Enable link. Quit your Chrome browser, then re-open it. 回答2: There's no way to run a Java applet within Chrome without NPAPI

No line found for nextline/int

依然范特西╮ 提交于 2019-12-13 03:04:28
问题 I try to let the user input some values in string and int in while loop, however, when I start I get no Line found I tried my best on my own to solve it but nothing worked for me. I wonder how I let the user input the values for the int and string. public static void main(String[] args) throws IOException { // TODO code application logic here int saldo = 10000; String anw = "j"; int cost; //create file File f = new File("C:\\Users\\ae65255\\Desktop\\file.txt"); try{ f.createNewFile(); System

compareTo and Strings using a Scanner

痴心易碎 提交于 2019-12-13 02:27:06
问题 I am implementing a form of leftist min heap, which stores arbitrary words by length. So, I have written a wrapper class for Scanner, and changed the compareTo, like so public class ScannerWrapper implements Comparable<String> //a Scanner, sc and a String, current public int compareTo(String str){ if(current.length() > str.length()) return -1; if(current.length() > str.length()) return 1; else return 0; } where current = sc.next() and is not the \n character. in this case, if I have

How to close a scanner without closing the underlying System.in?

邮差的信 提交于 2019-12-13 01:30:48
问题 If I close one scanner object and make a new one and try to read some more input, I'm getting the NoSuchElementException exception. My code works fine but it gives me a warning if I don't close the scanner. However, if I close it to get rid of the warning, I also close System.in ... How do I avoid this? Also, are there any consequences of not closing the scanner? EDIT: Here's my code: This is the NameAddressExists() method: public void NameAddressExists() { System.out.println("Enter name");

BufferedReader to read lines, then assign the new formed line's tokens to variables

百般思念 提交于 2019-12-13 01:23:31
问题 I have a text file that I need to modify before parsing it. 1) I need to combine lines if leading line ends with "\" and delete white spaced line. this has been done using this code public List<String> OpenFile() throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { String line; StringBuilder concatenatedLine = new StringBuilder(); List<String> formattedStrings = new ArrayList<>(); while ((line = br.readLine()) != null) { if (line.isEmpty()) { line = line