Results 1 to 10 of 10
Thread: HW help
- 10-03-2008, 08:15 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
HW help
I have a hw assignment that i have been working on for days and can't figure out what to do. I am reading a text with numbers with the scanner Filereader and i can't figure out how to add the numbers and so on. Here are the instructions i was givin. Any help would be very appreciated.
The program will read the file and then present the user with a menu. The user will then make a menu choice. Be sure to validate the user's input. The user can continue to use the program until the user opts to end the program. Ending the program is a menu choice.
Here is the output:
Make a selection from the list
1. Get the largest value
2. Get the smallest value
3. Get the sum of the values
4. Get the average
5. Get the number of values entered
6. End this program
Enter your choice --> 1
The largest value is 1000
- 10-03-2008, 08:16 PM #2
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
this is what i got so far
Java Code:# # import java.util.*; # import java.io.*; # # public class Lab5 # { # # static Scanner console = new Scanner(System.in); # # public static void main ( String[] args) # throws FileNotFoundException # # # { # # Scanner inFile = new Scanner(new FileReader("C:\\Random.txt")); # # int a; # String menu; # # menu = ("Make a selection from the list" + "/n" # +"1. Get the largest value" + "/n" # +"2. Get the smallest value" + "/n" # +"3. Get the sum of values" + "/n" # +"4. Get the average" + "/n" # +"5. Get the number of values entered" + "/n" # +"6. End this program" + "/n" # +"Enter your choice --->" + "/n"); # # a = console.nextInt(); # # while (inFile.hasNext()) # { # switch (a) # { # case 1: # # case 2: # # break; # # case 4: # # case 5: # # case 6: # System.out.print("Goodbye"); # System.exit(0);// Exit # # break; # # default: # System.out.print("Invalid Option"); # } # } # # # inFile.close(); # } # }Last edited by ryn21; 10-03-2008 at 08:35 PM.
- 10-03-2008, 08:27 PM #3
Do you have a problem, error or question?
Please use CODE tags when posting code. One of the icons above the input window will put them in the window.
- 10-03-2008, 08:35 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
oh sorry, ya im not sure how to do the operations on the menu options i have with the numbers from the file.
- 10-03-2008, 09:56 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
please someone help, i have no idea what to do next and its due soon.
- 10-03-2008, 10:22 PM #6
Start with a single step and add to it.
Where does your program read the contents of the file? Is it supposed to read all of the file at once? That will take a loop.
How is that to be done? Printed to STD output or ?present the user with a menu
How will the program get the user's choice?
Add println()s to the code to see the values of variables and to follow the program flow.Last edited by Norm; 10-03-2008 at 10:24 PM.
- 10-04-2008, 12:22 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
yes i have done that so far, here is what i have come up with. it is giving me an error on the while loop i tried to nest in the switch. I can not figure out how to do the math with the numbers from the text
Java Code:import java.util.*; import java.io.*; public class Lab5 { static Scanner console = new Scanner(System.in); public static void main ( String[] args) throws FileNotFoundException { Scanner inFile = new Scanner(new FileReader("Random.txt")); int a; int number = inFile.nextInt(); int sum = 0; System.out.println("1. Get the largest value"); System.out.println("2. Get the smallest value"); System.out.println("3. Get the sum of values"); System.out.println("4. Get the average"); System.out.println("5. Get the number of values entered"); System.out.println("6. End this program"); System.out.println("Enter your choice --->"); a = console.nextInt(); while (inFile.hasNextInt()) {if (a <= 6) switch (a) { case 1: case 2: case 3: while (num != 515) { sum += num; num = inFile.nextInt(); } System.out.println("Sum = " + sum); case 4: case 5: break; case 6: System.out.print("Goodbye"); System.exit(0);// Exit break; default: System.out.print("Invalid Option"); } } inFile.close(); } }
- 10-04-2008, 12:30 AM #8
Where did you post the error? I don't see it!it is giving me an error
If you are reading in text that consists of numeric digits, you can convert them to int by using a parse method of the Integer class.to do the math with the numbers from the text
Otherwise please explain.
- 10-04-2008, 12:44 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 16
- Rep Power
- 0
i have figured out the error but it does not need to be parsed. How do i make it return to the beginning of the program so the println menu returns after they perform an option.
- 10-04-2008, 02:12 AM #10


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks