• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 63
  • Last Modified:

How do I save data and use it next time

I am introducing myself as a beginner in Java.I have just made a program.My idea is to get an output depending on the ranges.So here is the program:
import java.util.Scanner;

class  Vedant
{
    public static void main(String[] args)
        {
            Scanner inputa=new Scanner(System.in);
            int b=inputa.nextInt();
            if(b > 0 && b < 20);
            int a=5;
            {
                System.out.println("Answer ="+a);
            }
            if(b<0)
            {
                System.out.println("Invalid");
        }
}
}

Open in new window


The code is perfect.I had doubts in it which were solved by this site.So what I want to do is get an output as 5 if the input is between 1-20.here is an example what I want to do:Suppose today I input it a number like 7 the answer should be 5.The next day I will input another no. like 10 the input will be 5 too.I want it to save and add the numbers automatically.So the first and second input's answer is 5 so I want the program to add up those two numbers automatically without editing the file everyday.Is there any way?I am just a beginner and don't understand much of it.

Also I want to design a statement like to assign multiple inputs as new scanners.Like Scanner a,b,c=new Scanner(System.in);
0
Vedant Naik
Asked:
Vedant Naik
4 Solutions
 
krakatoaCommented:
You can save calculations / user input (anything really), to a File and then read it back in later. Look at the File class and the rest of the .io API.
0
 
awking00Commented:
>>The code is perfect<<
Only as long as the input is a negative number or not greater than 19. Any program requiring input should work for any and all input. What happens on the third day when a 4 is entered (after the 10 and 7 from the first two days)? There are a number of ways to accumulate input that might require input from a saved file or an array or new scanner input, or other object. These activities would require new methods for reading a file, or summing an array, or prompting for new input, etc. You would probably also want to have a re-start method and, perhaps a delete or remove method and the like. The question seems to be of an educational nature and, as such, the experts are not allowed to provide specific solutions. Keep trying to make additional progress and will be happy to help with questions or suggestions as your program evolves.
0
 
CEHJCommented:
The code is perfect.

Far from it - it has a serious bug:

if(b > 0 && b < 20);

Open in new window


That's the end of the statement because it has a semicolon

To answer the bit about saving, you should probably use a simple database once you've got the code working. Using one will be more profitable in the end than reinventing one
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Vedant NaikAuthor Commented:
Yup But how should I get the database file working.Can I get a small example code please?
0
 
CEHJCommented:
https://docs.oracle.com/javadb/

should get you started
0
 
krakatoaCommented:
Closed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now