java.util.scanner

How to handle commas in the data of a csv in Java

蓝咒 提交于 2020-08-09 12:56:16
问题 I'm using scanner.delimiter to split my csv, with the delimiter being ",". However I have some data that includes commas in the data like "Monsters, Inc." However if I set the delimiter to "\",\"", then it crashes on everything else. Ideas that don't require me to write my own scanner.delimiter method? 回答1: I don't think scanner.delimiter will work for this kind of problem. If you have quotes in the data where the extra comas exist, you can either use a regular expression or code to solve

Java comparing string to regex - while loop

人盡茶涼 提交于 2020-07-16 02:48:13
问题 I want the user to enter a string and if the string does not match my regex expression then I want a message to be outputted and the user to enter a value again. The problem is even when the string matches the regex it is treating it as if it does not match. My regex: This should equal - Name, Name [[A-Z][a-zA-Z]*,\s[A-Z][a-zA-Z]*] My while loop: System.out.println("Enter the student's name in the following format - surname, forename: "); studentName = input.next(); while (!studentName.equals

Java comparing string to regex - while loop

心已入冬 提交于 2020-07-16 02:46:10
问题 I want the user to enter a string and if the string does not match my regex expression then I want a message to be outputted and the user to enter a value again. The problem is even when the string matches the regex it is treating it as if it does not match. My regex: This should equal - Name, Name [[A-Z][a-zA-Z]*,\s[A-Z][a-zA-Z]*] My while loop: System.out.println("Enter the student's name in the following format - surname, forename: "); studentName = input.next(); while (!studentName.equals

NoSuchElementException : No line found

北战南征 提交于 2020-07-15 08:42:47
问题 I am working on a simple project, I took it from a website that gives some challenge to improve my coding skill in java. import java.util.Scanner; public class Test1 { public void test() { Scanner in = new Scanner(System.in); System.out.println("enter something :"); String str = in.nextLine(); StringBuilder sb = new StringBuilder(str); if (str.isEmpty()) { System.out.println("you should write something"); } if(str.length()<=30){ System.out.println("reverse : "+sb.reverse()); }else { System

Explanation regarding skipping of alternate lines while reading from a file?

岁酱吖の 提交于 2020-07-04 02:46:48
问题 I just want to know, have I interpreted the code in right manner? The file Bharath.txt has the following content: Hello ! B . We created your file succesfully . We have updated you file , Sir ! Bharath Nikhil Friends Indeed Consider the below code: import java.util.Scanner; import java.io.*; class FilInpStr { public static void main(String arg[]) throws Exception { FileInputStream obj=new FileInputStream("Bharath.txt"); Scanner input=new Scanner(obj); String n=""; while((n=input.nextLine())!

How to use the same Scanner across multiple classes in Java

混江龙づ霸主 提交于 2020-06-14 04:18:34
问题 I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to help me. Here are the Scanners that I have made in my main class: Scanner in = new Scanner(System.in); System.out.println("Enter a filename"); String filename = in.nextLine(); File InputFile = new File (filename); Scanner reader = new Scanner

How to use the same Scanner across multiple classes in Java

南楼画角 提交于 2020-06-14 04:13:06
问题 I have a program that uses multiple classes, I want the other classes to be able to access the same scanner that I have declared in the main class, I assume it would be done using some sort of get method, however I am unable to find any resources to help me. Here are the Scanners that I have made in my main class: Scanner in = new Scanner(System.in); System.out.println("Enter a filename"); String filename = in.nextLine(); File InputFile = new File (filename); Scanner reader = new Scanner

Why does Scanner#nextInt inside for loop keep throwing an exception?

不打扰是莪最后的温柔 提交于 2020-06-08 17:14:06
问题 I have been learning JAVA and i have a small doubt about the code : class apple { public static void main(String[] args) { int[] num = new int[3]; Scanner input = new Scanner(System.in); for (int i = 0; i < num.length; i++) { try { num[i] = input.nextInt(); } catch (Exception e) { System.out .println("Invalid number..assigning default value 20"); num[i] = 20; } } for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } } I have written small program to handle exception, if user