Java program which opens a file containing integers, and print, counts and displays in output dialog box.(Using JOptionPane)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Java program which opens a file containing integers, and print, counts and displays in output dialog box.(Using JOptionPane)



I have to create a java program which opens a file containing integers. An input dialog box asks the use the name of the input file, and then an output dialog box shows the values read from the data file, each on a different line, and then the number of values from the file. I am confused
as to what is missing from my code. The input dialog box appears but I am completely stuck after that.


import java.io.*;
import javax.swing.*;
import java.util.Scanner;

public class prob2

public static void main (String args) throws IOException

String name, out="", out2="" ;
int quantity=0, val;
name=JOptionPane.showInputDialog("Enter the name of the file to be opened: ");
Scanner inputFile= new Scanner(new FileReader(name));

while(inputFile.hasNextInt());

val=inputFile.nextInt();
out= val+"n";
quantity++;

JOptionPane.showMessageDialog(null,
val + quantity);
inputFile.close();






while (inputFile.hasNextInt()); <-- wonder what that little ; will do 🤔
– MadProgrammer
Aug 10 at 4:00


while (inputFile.hasNextInt());


;





Welcome to Stack Overflow! I noticed that your question is still "open" - as you didn't accept an answer. Please have a look and decide if you want to accept an answer. Or let me know if there is something I can do to enhance my input to make it accept worthy. Accepting helps future readers to determine if the problem is solved, and shows appreciation for the people who took the time answering you. Thanks!
– GhostCat
Aug 13 at 9:31




2 Answers
2



Your initial problem is right here


while(inputFile.hasNextInt()); // <- Or here actually...



This is basically setting up an infinite loop instead of executing the block of ode you want



Start by removing the ; at the end of the line...


;


while(inputFile.hasNextInt())

val=inputFile.nextInt();
out= val+"n";
quantity++;



Once you've that, you will to change


JOptionPane.showMessageDialog(null,
val + quantity);



to


JOptionPane.showMessageDialog(null,
out + quantity);



to overcome the compiler error



And finally, you will need to change...


out = val + "n";



in your while-loop to


while-loop


out += val + "n";



so that you are actually concatenating the result



(and yes, we should be using StringBuilder, but I think we can let that one slide for now)


StringBuilder





thank you so much! whoops stupid mistakes of me, but once i made those edits, the output dialog box only prints the first value from the text file I chose, and then the quantity on the next line. how do i get it to print ALL the values from the file and not just the last value from my file??
– tsb97
Aug 10 at 4:11





"whoops stupid mistakes of me" - Welcome to my every day 😓🤣. "the output dialog box only prints the first value from the text file" - I made an amendment to the answer which should solve it, see the last 5 lines or so.
– MadProgrammer
Aug 10 at 4:12





you are an absolute life saver, thank you so much!! class hadn't touched upon StringBuilder yet and I just couldn't figure it out. it worked perfectly
– tsb97
Aug 10 at 4:15





You don't "need" StringBuilder and given all the other issues you seem to be having, it won't provide you any additional benefit - just understand that StringBuilder will provide you with a performance boost over performing String concatenation within a loop like you're doing
– MadProgrammer
Aug 10 at 4:17



StringBuilder


StringBuilder


String





I'm still new to learning java, but thank you, you were a lot of help! I'll learn StringBuilder and figure out how to use that next, I was just trying to use what I knew already to figure out this program 😊
– tsb97
Aug 10 at 4:20



You have to collect the input you are reading in and that you want to later print.



For example by creating a StringBuilder instance and appending the content you wish to present to the user.



When you are done reading you pass builder.toString() to another option pain as message.





thank you for your help! i hadn't yet learnt StringBuilder, so I was trying to build my program with what I already knew :(
– tsb97
Aug 10 at 4:16





Sure. In case this works for you, please consider accepting the answer at some point.
– GhostCat
Aug 10 at 4:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard