java.util.scanner

Java Scanner issues (JFrame)

↘锁芯ラ 提交于 2019-12-31 07:03:09
问题 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

Java Scanner issues (JFrame)

落爺英雄遲暮 提交于 2019-12-31 07:02:12
问题 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

Reading a file and storing names and numbers in two arrays

故事扮演 提交于 2019-12-31 07:01:12
问题 I'm working on a program that reads a file and stores the names and scores in two separate arrays, but I'm struggling. This is what I have so far. I created an array for names called names, but I'm confused how I would copy the names into each index of the array. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerReadFileSplit { public static void main(String[] args) { File file = new File("NamesScore.txt"); String[] names = new String[100

Reading a file and storing names and numbers in two arrays

落爺英雄遲暮 提交于 2019-12-31 07:00:28
问题 I'm working on a program that reads a file and stores the names and scores in two separate arrays, but I'm struggling. This is what I have so far. I created an array for names called names, but I'm confused how I would copy the names into each index of the array. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerReadFileSplit { public static void main(String[] args) { File file = new File("NamesScore.txt"); String[] names = new String[100

java util scanner throwforunknown source error (cant type input)

人走茶凉 提交于 2019-12-31 05:37:07
问题 I have a problem with a scanner and I don't really know what's wrong. I have a Circle class and I want to make check loop for its radius on the constructor. Here is the code: Circle(double x, double y, String color, double radius) { super(x, y, color); // constructor from class shape which is extended by circle Scanner r = new Scanner(System.in); while (radius <= 0) { System.out.println("radius has to be > 0.....Give radius again"); if (r.hasNextDouble()) { radius = r.nextDouble(); } else { r

Why get i InputMismatchException when i use scanner sc.nextDouble()

穿精又带淫゛_ 提交于 2019-12-31 04:36:13
问题 I'd like to read data from a txt file, but I get InputMismatchException when I call nextDouble() method. Even though I am using the useLocale method, but it doesn't work. The txt file first line is: 1;forname;1.9 public class SimpleFileReader { public static void main(String[] args){ readFromFile(); } public static void readFromFile(){ try { int x = 0; File file = new File("read.txt"); Scanner sc = new Scanner(file).useDelimiter(";|\\n"); sc.useLocale(Locale.FRENCH); while (sc.hasNext()){

Infinite loop on Scanner.hasNext, reading from a file

流过昼夜 提交于 2019-12-31 02:54:12
问题 I'm apparently facing an infinite loop on while(input.hasNext()) , as in following code File file = new File("data.txt"); Scanner input = new Scanner(file); int sum = 0; while (input.hasNext()) { if (input.hasNextInt()) { sum += input.nextInt(); } } System.out.println(sum); Some related questions explain why it always returns true if input stream is System.in , however I'm scanning through a File . Please let me know where I'm going wrong. I'm attempting to calculate the sum of unique integer

Can you jump a scanner to a location in file or scan backwards?

不想你离开。 提交于 2019-12-31 01:20:07
问题 I have a very large text file and I need to gather data from somewhere near the end. Maybe Scanner isn't the best way to do this but it would be very wasteful to start at the top and grab 6000 lines before getting to the part of the file I am interested in. Is there a way to either tell Scanner to jump to say 7/8ths down the document or start from the bottom and scan upwards grabbing line by line? Thanks 回答1: The underlying input source for a java.util.Scanner is a java.lang.Readable. Beyond

Can you jump a scanner to a location in file or scan backwards?

蓝咒 提交于 2019-12-31 01:20:07
问题 I have a very large text file and I need to gather data from somewhere near the end. Maybe Scanner isn't the best way to do this but it would be very wasteful to start at the top and grab 6000 lines before getting to the part of the file I am interested in. Is there a way to either tell Scanner to jump to say 7/8ths down the document or start from the bottom and scan upwards grabbing line by line? Thanks 回答1: The underlying input source for a java.util.Scanner is a java.lang.Readable. Beyond

Java Scanner class reading strings [duplicate]

佐手、 提交于 2019-12-30 10:23:26
问题 This question already has answers here : Java Scanner class reading strings (5 answers) Closed 5 years ago . I got the following code: int nnames; String names[]; System.out.print("How many names are you going to save: "); Scanner in = new Scanner(System.in); nnames = in.nextInt(); names = new String[nnames]; for (int i = 0; i < names.length; i++){ System.out.print("Type a name: "); names[i] = in.next(); } System.out.println(names[0]); When I run this code, the scanner will only pick up the