java.util.scanner

Having problems reading from and deleting certain lines from a file?

*爱你&永不变心* 提交于 2019-11-29 18:00:52
The point of this program is to remove certain sports teams and their members from a text file, then overwrite the original file with the new set of values. This is attempted by reading the values into an array, then looping through the array and deleting the name of the team and the next 2 lines but for some reason it stops going through the array after the index. I'm stuck, so any help would be great Code: private void RemoveSportsTeamButtonActionPerformed(java.awt.event.ActionEvent evt) { String ChosenTeam = ""; ChosenTeam = JOptionPane.showInputDialog("What Team Do you want to remove?");

How to use Scanner to read silently from STDIN in Java?

戏子无情 提交于 2019-11-29 17:44:35
问题 I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps . 回答1: The class java.io.Console may be useful: System.console().readPassword(); This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns

FileNotFoundException even when the file is there

浪尽此生 提交于 2019-11-29 17:21:00
public StormAnalysis(){ try { fScanner = new Scanner(new File("tracks1949to2010_epa.txt")); while(fScanner.hasNextLine()){ System.out.println(fScanner.nextLine()); } } catch (FileNotFoundException e) { System.out.println("File not found. Try placing the tracks1949to2010_epa.txt in the same folder as StormAnalysis.java"); e.printStackTrace(); } } The above is my code (and I also have an image of the error : http://folk.uio.no/arnabkd/test/images/error-code-task.jpg As you can see, the txt file is in the same folder as the StormAnalysis.java file. In addition, the code works if I change the file

Java - Scanner Class - Skipping over the first line when reading a textfile

久未见 提交于 2019-11-29 17:17:55
When using a Scanner object to read from a textfile, I want it to skip over the very first line in the file. How would I do achieve this? Just use file.nextLine() before your while loop. This will skip the first line, as explained in the JavaDoc . And a note about your naming. The Java language has widely accepted conventions. Class Names always start with an upper case letter, and variable names always start with a lower case letter (except constants, but don't worry about that now). Read more here . Add the below code before the while loop to skip the first line. if(file.hasNext()==true) {

Scanner objects in methods and NoSuchElementException [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-29 17:17:07
This question already has an answer here: java.util.NoSuchElementException - Scanner reading user input 2 answers I have really tried to find the answer through the threads but still hope to get some feed back. The code below is bad style I think but I don't know why it shoot me a java.util.NoSuchElementException after enter the number since I make two Scanner objects for two methods and I should be able to start a new input. And if I erase the input.close() in inputAndPrintNumber(), it works and compile correctly. I really hope to know why and how to fix it if I still use two Scanner obj and

Creating a Java Program to Search a File for a Specific Word

人盡茶涼 提交于 2019-11-29 16:02:42
问题 I am just learning that language and was wondering what a more experience Java programmer would do in the following situation? I would like to create a java program that will search a specified file for all instanced for a specific word. How would you go about this, does that Java API come with a class that provides file scanning capabilities or would i have to write my own class to do this? Thanks for any input, Dom. 回答1: The java API does offer the java.util.Scannerclass which will allow

Java Scanner why is nextLine() in my code being skipped? [duplicate]

若如初见. 提交于 2019-11-29 15:51:25
This question already has an answer here: Scanner is skipping nextLine() after using next() or nextFoo()? 15 answers Scanner kb = new Scanner(System.in); System.out.println("Inserting L"); int L = kb.nextInt(); System.out.println("Inserting N"); int N = kb.nextInt(); System.out.println("Inserting x"); String x = kb.nextLine(); System.out.println(x); System.out.println("You inputed L,N,x"); Why is there no prompt for nextLine() in this code? dogbane Use: String x = kb.next(); instead of kb.nextLine() . This is because nextInt() reads just the number, not the end of line or anything after the

Why is this code getting a Java NoSuchElement exception?

杀马特。学长 韩版系。学妹 提交于 2019-11-29 15:28:55
I have traced through this code and can't figure out how to fix it. When running the code why wouldn't the user be prompted for input rather than Java determining that there is no input? Error trace below. import java.util.*; public class SortAsInserted { public static void main(String[] args) { int array_size = GetArraySize(); //System.out.println(array_size); String[] myArray = new String[array_size]; for (int i = 0; i < array_size; i++){ String next_string = GetNextString(); System.out.println(next_string); } } //public static String[] SortInsert(String nextString){ //} public static int

Infinite While Loop When InputMidmatchException is caught in try-catch block [duplicate]

[亡魂溺海] 提交于 2019-11-29 15:11:25
This question already has an answer here: try/catch with InputMismatchException creates infinite loop 7 answers I keep getting my code caught in an infinite while loop. It is nothing to advanced, but i can not figure it out for the life of me! Someone Please help I have purplosely just re created the specific error without all of the if statements i have in my actual program. package bs; import java.util.InputMismatchException; import java.util.Scanner; public class bs { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean continueVar = true; while (continueVar

“Cannot be resolved to a type” when attempting to use Scanner

≯℡__Kan透↙ 提交于 2019-11-29 15:01:16
when i run following code it shows error that scanner cannot be resolved to type. i checked that jre is installed and version is 1.7 what else do i need to check ? please help. public class student { String name; int rollno; public void get(String nm, int rno) { name=nm; rollno=rno; } public void display() { System.out.println("Name of student is :" +name); System.out.println("Roll no of student is :" +rollno); } public static void main(String args[]) { int i ; int r1; String n1; student obj[]= new student[10]; Scanner sc=new Scanner(System.in); for(i=0;i<10;i++) { obj[i]= new student(); } for