java.util.scanner

Java scanner reading garbage

て烟熏妆下的殇ゞ 提交于 2019-12-25 19:01:51
问题 I am reading a text file using java Scanner. try { while(sc.hasNextLine()) { //Read input from file inputLine = sc.nextLine().toUpperCase(); System.out.println(inputLine); } The above gives below output while my text file only includes "aabbcc". How to avoid scanner from scanning the garbage? Thanks. {\RTF1\ANSI\ANSICPG1252\COCOARTF1265\COCOASUBRTF210 {\FONTTBL\F0\FSWISS\FCHARSET0 HELVETICA;} {\COLORTBL;\RED255\GREEN255\BLUE255;} \PAPERW11900\PAPERH16840\MARGL1440\MARGR1440\VIEWW10800

try catch for Scanner

牧云@^-^@ 提交于 2019-12-25 09:35:09
问题 I am using Scanner to get user input. If the user inputs a name, I add that to the ArrayList . If the user does not enter a name then I want to throw an exception, but I want to continue the loop that gets the answer. for(int i = 0; i < totalLanes; i++){ runArr.add(this.addRacer()); } public static String addRacer() throws NullPointerException{ System.out.print("Enter a name for a racer: ");//Method uses try catch to catch a NullPointerException. Scanner sc = new Scanner(System.in); String

Reading a CSV file using another class then login java

别来无恙 提交于 2019-12-25 08:33:37
问题 I am trying to read a CSV file from another class then take the details and compare them with the login information given by the user in the main class. I can do it by putting everything in the main class but it doesn't look very neat. --Main-- package supscription.service; /** * * @author jakec */ import java.util.*; public class SubscriptionService { /** * @param args the command line arguments */ public static void main(String[] args) { String username; String password; Scanner input = new

Encountering some bugs regarding the Scanner and an unknown source in my code. No idea how to fix it

旧城冷巷雨未停 提交于 2019-12-25 05:33:37
问题 I'm trying to teach myself some java and this is my code, but these are the errors that i'm getting as soon as i make my first input telling the program which conversion mode to go into. Any help is appreciated import java.util.Scanner; public class TempConverter { public static void cToF(){ double C=0, F=0, mode= 0; Scanner input = new Scanner(System.in); System.out.println("Enter your temperature in Celsius."); C = input.nextDouble(); input.close(); F = ((double)9/(double)5*C)+32; System

Scanner Class InputMismatchException and Warnings

江枫思渺然 提交于 2019-12-25 03:47:25
问题 I am having trouble running this program. The program takes an expression such as "A = 7" and puts the variable A (String) and the number 7 in a Map container. I'm not if i'm not parsing the string correctly which is causing the error or for some other reason. Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt

Read integers from txt file and storing into an array

半城伤御伤魂 提交于 2019-12-25 02:22:40
问题 I'm having issues reading and storing only integers from a text file. I'm using a int array so I want to do this without list. I'm getting a input mismatch exception, and I don't know how I should go about correcting that issue. The text files being read from also include strings. public static Integer[] readFileReturnIntegers(String filename) { Integer[] array = new Integer[1000]; int i = 0; //connect to the file File file = new File(filename); Scanner inputFile = null; try { inputFile = new

Scanner is skipping nextLine() after using next() or nextFoo()?

和自甴很熟 提交于 2019-12-25 01:42:39
问题 I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println("Enter numerical value"); int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st string"); String string1 = input.nextLine(); // Read 1st string (this is skipped) System.out.println("Enter 2nd string"); String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value) The problem is that after

Reading a file using FileReader and Scanner

雨燕双飞 提交于 2019-12-24 15:25:11
问题 I am a Java beginner and have read similar questions but still I dont get why my code is showing a FileNotFound Exception. My file is in the same directory. My code is: import java.io.*; import java.util.Scanner; public class reader { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); double y = in.nextDouble(); float g = in.nextFloat(); String a = in.next(); File file = new File("v.txt"); System.out.println(x + "" + y + "" + g + "" + a);

Java java.util.regex.MatchResult counter problems with Scanner

人盡茶涼 提交于 2019-12-24 13:25:57
问题 I'm using a java.util.Scanner to scan all occurrences of a given regex from a big string. Scanner sc = new Scanner(body); sc.useDelimiter(""); String match = ""; while(match!=null) { match = sc.findWithinHorizon(pattern, 0); if(match==null)break; MatchResult mr = sc.match(); System.out.println("Match string: "+mr.group()); System.out.println("Match string using indexes: "+body.substring(mr.start(),mr.end()); } The strange thing is that after a certain number of scans, group() method returns

Assigning 1 line of a txt file to a string via scanner

…衆ロ難τιáo~ 提交于 2019-12-24 13:12:37
问题 As the title explains I'm trying to get each line assigned to a string (My ultimate goal would be to have it pull a line from a text file, then use the line below as an answer, then to repeat this until the file is finished) right now I've only got it to assign the whole file to a string (line). Here's my code - import java.io.*; import java.util.Scanner; import java.lang.*; import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { // Location of file to