java.util.scanner

Using the Scanner class in Java to get specific digits of an integer [duplicate]

拈花ヽ惹草 提交于 2019-12-02 14:49:36
问题 This question already has answers here : How to get the separate digits of an int number? (26 answers) Closed 3 years ago . I just started learning Java (two months and counting), and I still have lots of questions and a whole lot to learn. Right now I want to use the Scanner class to "divide" an integer into its digits. I'll explain myself better with an example: I request the user to type a four-digit integer, say 8919 . What I want to do is to use the Scanner class to divide that integer

Trying to read binary file as text but scanner stops at first line

老子叫甜甜 提交于 2019-12-02 13:53:43
问题 I'm trying to read a binary file but my program just stops at first line.. I think it's because of the strange characters the file has..I just want to extract some directions from it. Is there a way to do this?.. public static void main(String[] args) throws IOException { Scanner readF = new Scanner(new File("D:\\CurrentDatabase_372.txt")); String line = null; String newLine = System.getProperty("line.separator"); FileWriter writeF = new FileWriter("D:\\Songs.txt"); while (readF.hasNext()) {

How can I create a loop for user input (until the user enters valid input)? [duplicate]

假装没事ソ 提交于 2019-12-02 13:42:09
This question already has an answer here: How to use Scanner to accept only valid int as input 6 answers My program should ask user to enter the amount they want to withdraw from their account and calculating the current balance after withdrawing. The requirement for withdrawing is minimum 100 and maximum 1000. If the user enters wrong input the program should re-promt and ask user to enter amount again. This process will keep repeating until the user puts right amount. Once the right amount is choosen it should calculate and display the current balance. This is how I tried, but I failed to

Printing a replaced line in a new text file

纵然是瞬间 提交于 2019-12-02 12:57:28
I am trying to edit a matlabfile and replace some of the coding parts init in some specific lines. However, using the format below to make the changes it wont change the line context at all. (it will print the same old line). Any idea what am I doing wrong? 'replaceAll' is not suitable to replace some words with some others in the lines? Thanks in advance. try { PrintWriter out = new PrintWriter(new FileWriter(filenew, true)); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains("stream.Values(strmatch('Test',stream.Components

Java: Scanner stopping at new line

╄→гoц情女王★ 提交于 2019-12-02 11:24:26
问题 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

Placing correct discs Connect Four java game with drawing panel

拥有回忆 提交于 2019-12-02 11:23:35
I'm trying to make a Connect Four Game in Java by using user input and drawing panel. Two people switch off turns typing the column they want their piece to go in. I'm having trouble making it so discs already placed stay on the board the next turn (because the a new drawing panel pops up with the updated board each time)(we tried using GUI but failed). I am also having trouble switching off turns and making the color change each time. Thank you so much, here is what I have so far. import java.util.*; import java.awt.*; public class ConnectFour{ public static void main(String[] args){ Scanner

NullPointerException When Entering Data Into a Dynamic Array from Text File

北城以北 提交于 2019-12-02 11:14:37
I am making a voting system where the user logs in with their student number and makes a selection. Once someone has voted, they cannot be able to log in again. I made an object; Students, containing a String for the student number and a boolean for whether or not that student number has already voted (both being private). I made a dynamic array of this type as to accept a given amount of students off of a text file that is read through the use of a Scanner. However, when I try to populate the student number string within the object array, I get a NullPointerException. The Scanner IS reading

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

本小妞迷上赌 提交于 2019-12-02 10:45:23
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 entering the numerical value, the first input.nextLine() is skipped and the second input.nextLine() is

Why is nextDouble() from the Scanner method sending me “Exception”

五迷三道 提交于 2019-12-02 10:38:06
I'm suppose to enter 2 numbers, one int that is the amount to withdraw and one double which is the balance (with a space between them). Since every withdraw charges a fee of 0.5, balance must be a double. And thats what must be printed. I get error at nextDouble, why? I have just 1 month coding, I thought this was going to be a piece of cake, I think BASIC syntax ruined me 30 years ago :( import java.util.Scanner; public class Test { public static void main(String[] args) { //init variables int amount; double balance; //insert amount and balance Scanner input = new Scanner (System.in); amount

Using the Scanner class in Java to get specific digits of an integer [duplicate]

这一生的挚爱 提交于 2019-12-02 10:22:44
This question already has an answer here: How to get the separate digits of an int number? 26 answers I just started learning Java (two months and counting), and I still have lots of questions and a whole lot to learn. Right now I want to use the Scanner class to "divide" an integer into its digits. I'll explain myself better with an example: I request the user to type a four-digit integer, say 8919 . What I want to do is to use the Scanner class to divide that integer and to assign each one of its digits to a variable; i.e. a = 8 , b = 9 , c = 1 and d = 9 . I positively know that it can be