7 Replies - 29014 Views - Last Post: 31 October 2009 - 08:17 AM Rate Topic: -----

#1 coolies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 09

Prompt the user for a file name.

Posted 30 October 2009 - 04:49 PM

How do i get javac to prompt a user for a file name and then read this file of numbers for an array?

please help
Is This A Good Question/Topic? 0
  • +

Replies To: Prompt the user for a file name.

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Prompt the user for a file name.

Posted 30 October 2009 - 04:51 PM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prompt the user for a file name.

Posted 30 October 2009 - 04:52 PM

May be the not the good forum you just discovered
If you want code: Google
If you have problem with the code YOU wrote we will be glad to help

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 coolies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 09

Re: Prompt the user for a file name.

Posted 30 October 2009 - 04:53 PM

I have an issue, i need to start the program by asking a user to enter a filename, but i cannot do this, so i cannot do the next step. :(
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Prompt the user for a file name.

Posted 30 October 2009 - 05:50 PM

public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   System.out.print("Enter filename: ");
   String filename = in.nextLine();
}



OK let see what you will go from there
:code:
Was This Post Helpful? 0
  • +
  • -

#6 coolies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 09

Re: Prompt the user for a file name.

Posted 31 October 2009 - 03:26 AM

ok, this is what i had

import java.util.*;
import java.io.*;
import java.lang.*;

public class Assignment4{
		public static String data;
		public static ArrayList <Integer> store = new ArrayList <Integer>();
		public static Scanner scanner = new Scanner (System.in);
		public static int average;
		public static double standarddev;
		public static int devMean = 0;
	   
		public static void main(String[] args) throws Exception{
				readfile(); // calls readfile
				calculateAverage(); //calls calculateAverage
		}
	   
		public static void readfile() throws Exception {
				System.out.println ("Please input data.txt file");
				data = scanner.nextLine();
			   
				File inputFile = new File("data.txt"); // inputs file
				Scanner reader = new Scanner(inputFile);		
					   
						while (reader.hasNext()){		   //scanner reads file			
								store.add(reader.nextInt()); //scanner stores each integer separately in the array list
						}
		}	  

		public static void calculateAverage(){
		int sum = 0;


I'm unsure what to change tbh.

This post has been edited by coolies: 31 October 2009 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 mab4   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 31-October 09

Re: Prompt the user for a file name.

Posted 31 October 2009 - 07:25 AM

Hey

while I was googling to do my assignment ,I found this forum. interestingly, it seem one of my classmate struggling to do it like me.

Anyway, to ask the user for the file simply do as below

View Postpbl, on 30 Oct, 2009 - 04:50 PM, said:

public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   System.out.print("Enter filename: ");
   String filename = in.nextLine();
}



OK let see what you will go from there
:code:


then you need to assign what in scanner to File method
	File inputFile = new File(filename);
	Scanner reader = new Scanner(InputFile);


This post has been edited by mab4: 31 October 2009 - 07:30 AM

Was This Post Helpful? 1

#8 ts230   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 225
  • Joined: 11-July 09

Re: Prompt the user for a file name.

Posted 31 October 2009 - 08:17 AM

YOu could use a JFileChooser and then get the file, Open a BufferedStream, read the All the data, put it into a String and then parse it by calling String.split(",") on the string containing all that data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1