java.util.scanner

Java Scanner Delimiter Usage

自古美人都是妖i 提交于 2019-12-02 10:15:48
I'd like to specify a delimiter for a scanner that splits on some pattern, but doesn't remove that pattern from the tokens. I can't seem to make this work, as anything that is identified by the regex also gets eaten as part of the delimiter. Any suggestions? My specific problem, I have file that looks like: text/numbers mix numbers numbers text/numbers mix numbers numbers numbers . . I'd like to split out from the text/numbers mix+rows until the next text/numbers mix. I have the regex to identify them, but as stated, using that as the delimiter eats part of what I want. EDIT: code addition:

Java Scanner issues (JFrame)

£可爱£侵袭症+ 提交于 2019-12-02 10:01:33
I am trying to use a scanner to edit the level of my tower defense game. However it will not update the level (the tile images) to that of the custom file (0 is grass 1 is stone -1 is nothing, etc.). I have found the error but how do i fix it, what do i need to add/change to get rid of this? java.lang.NullPointerException at Levels.loadLevels(Levels.java:11) at Window.define(Window.java:28) at Window.paintComponent(Window.java:44) line 11: for(int y=0;y<Window.room.block.length;y++) { line 28: levels.loadLevels(new File("levels/level1.level")); line 44: define(); This is the scanner file:

Java Scanner no line found, and then Scanner closed error?

旧时模样 提交于 2019-12-02 09:26:38
I have Java code that asks for user input and then stores this data in a string variable. The below function is part of a class called 'number' and is called in the main function. public static void setVal(int i){ Scanner readIn = new Scanner(System.in); //while (readIn.hasNextLine()){ str = readIn.nextLine(); numCheck = false; if (i == 1){ while (!numCheck){ if (str.contains(" ")){ System.out.println("Please input a single item."); str = readIn.nextLine(); } else if (!isNumeric(str)){ System.out.println("Please input a valid number."); str = readIn.nextLine(); } else { numCheck = true; value

Adding an object from file into program

送分小仙女□ 提交于 2019-12-02 08:52:47
I'm programming a game and in the program I need to add new enemies based off of a file. Right now my problem is that I've run into an infinite while loop when trying to read this file. I'm relatively new to programming so I'm not exactly sure how to fix this. Here is the problem code. An example of how the entry in the file looks is: "Troll,6,4,1". Thank you for your help. try { Scanner input = new Scanner(new File(filename)); while(input.hasNext()); { input.useDelimiter(",|\n"); String name = input.next(); int strength = input.nextInt(); int speed = input.nextInt(); int numVials = input

Java Scanner Delimiter Usage

我只是一个虾纸丫 提交于 2019-12-02 07:52:33
问题 I'd like to specify a delimiter for a scanner that splits on some pattern, but doesn't remove that pattern from the tokens. I can't seem to make this work, as anything that is identified by the regex also gets eaten as part of the delimiter. Any suggestions? My specific problem, I have file that looks like: text/numbers mix numbers numbers text/numbers mix numbers numbers numbers . . I'd like to split out from the text/numbers mix+rows until the next text/numbers mix. I have the regex to

Unreported exception java.io.FileNotFoundException;?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 07:49:58
I want to open a file and scan it to print its tokens but I get the error: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown Scanner stdin = new Scanner (file1); The file is in the same folder with the proper name. import java.util.Scanner; import java.io.File; public class myzips { public static void main(String[] args) { File file1 = new File ("zips.txt"); Scanner stdin = new Scanner (file1); String str = stdin.next(); System.out.println(str); } } The constructor for Scanner you are using throws a FileNotFoundException which you must catch at compile

Stumped by FileNotFound exception

谁说胖子不能爱 提交于 2019-12-02 07:20:41
All I want to do is import data from a text file. The file exists at C:\temp\text.txt. However, I keep getting a file not found error. What the heck am I doing wrong??? public static void main(String[] args) throws IOException { String mFilename = "C:\\temp\\text.txt"; Scanner myDataStream = new Scanner(new File(mFilename));} For the sake of giving this question an answer: Googling brought up a case where someone had file extensions hidden and his file was actually text.txt.txt . Check if it's not your case. You can display extensions in Windows 7 by going in Organize > Folder and search

Java: Scanner stopping at new line

别等时光非礼了梦想. 提交于 2019-12-02 07:16:57
I am trying to scan through text files and add them to a map, the map and everything is working. However, the scanner seems to be stopping when it comes to a 'enter' in the text file, or a blank line. This is my problem here is my block of code for the scanner/mapper class OneButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent evt) { final JFileChooser oneFC = new JFileChooser(); oneFC.showOpenDialog(AnalysisFrame.this); String newLine = null; oneFC.getName(null); int returnVal = 0; File fileOne = oneFC.getSelectedFile(); Scanner input = null; try {

Splitting up data file in Java Scanner

六月ゝ 毕业季﹏ 提交于 2019-12-02 06:04:07
问题 I have the following data which I want to split up. (1,167,2,'LT2A',45,'Weekly','1,2,3,4,5,6,7,8,9,10,11,12,13'), to obtain each of the values: 1 167 2 'LT2A' 45 'Weekly' '1,2,3,4,5,6,7,8,9,10,11,12,13' I am using the Scanner class to do that and with , as the delimiter. But I face problems due to the last string: ('1,2,3,4,5,6,7,8,9,10,11,12,13') . I would hence like some suggestions on how I could split this data. I have also tried using ,' as the delimiter but the string contains data

Java Scanner to print previous and next lines

夙愿已清 提交于 2019-12-02 05:59:11
问题 I am using 'java.util.Scanner' to read and scan for keywords and want to print the previous 5 lines and next 5 lines of the encountered keyword, below is my code ArrayList<String> keywords = new ArrayList<String>(); keywords.add("ERROR"); keywords.add("EXCEPTION"); java.io.File file = new java.io.File(LOG_FILE); Scanner input = null; try { input = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int count = 0; String previousLine = null; while(input.hasNext()){