hello im new in java and i have to do simple program and can read from txt file

Recommended Answers

All 10 Replies

What does the text file look like?

Member Avatar for joankim

You should use the scanner class for that.
First import java.util.*;

As you are new to java, I'll give you a sample of how to do it. Normally, you would want to declare it before your constructor. It needs to hava a varible name that can be anything. I always use "in". Also, you want you text file to be in the folder where all the files in your program are saved. And this must be done inside a try{}catch{}
The example will take ints and store them in an arraylist.

Look here for info about command in the scanner class: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html

java.util.Scanner;
public class classname{
Scanner in;
ArrayList<Integer> list = new ArrayList<Integer>();


public classname{
try{
in = new Scanner(new File(filename.txt));
while(in.hasNext()){
list.add(in.nextInt());
    }
}catch(Exception e){System.out.print("Error");}
}
}

thanks.. it helps me alot .. is it okey if i seperate the string and int in 2d array so i can calculate the value in each column. because from the string i have to count how many time it have be write and from the int i have to calculate the value

Member Avatar for joankim

Firstly, JamesCherrill reminded me that it might be a good idea not to take shortcuts, and teach you the best way to do this. I tend to be more precice in my own code, but you should know the right way to do it. In catching, you should use catch

(Exception e){System.out.print(e.printStackTrace());}

Instead. So you will know why you got an error. For your both strings and values in the same array index, I would go with ArrayList instead. I'll show you a quick example of how to do that. I use ArrayLists containing Items. Declared like this:

ArrayList<Item> list = new ArrayList<Item>();

Your arrayList called list will now contain objects of type Item. You should make a seperate class to define Item. Here is an example of how I do this. Note the 'getter' methods implemented so that you can get either the value OR the string at a specific index by simply calling list.get(i).getId()

public class Item {
    public int myId;
    public int myInv;

    public Item(int id, int inv){
        myId = id;
        myInv = inv;
    }

    public int getId(){
    return myId;
    }

    public int getInv(){
    return myInv;
    }

    public int compareTo(Item other){
    return myId - ((Item)other).myId;
    }

    public String toString(){
    return ("The ID number is: " + myId + ", and the inventory left is: " + myInv);
    }
}

You might want to change the value names and types of myId and myInv. CompareTo method is for sorting, and is optional. toString method is for being able to print your list. Simply print your list as usual in any class, and it will look for your toString method. Within a loop, you can just go System.out.println(list.get(i) and it will print whatever is inside your toString method (for the current index (i))

To add your different values to item, follow this example:

in = new Scanner(new File("file50.txt"));
    while(in.hasNextInt()){
        myId = in.nextInt();
        myInv = in.nextInt();
        in.nextLine();
        myStore.add(new Item(myId, myInv));

From what I can tell from your last post, it looks like you want the each string to represent a word in your file, and the int to be how many times this specific string (word) occured? If so, I will give you a little bump start for how to do this. I would follow this example. You will need more variables in there, and it is not finished. Remember, for each time you go in.next(), your scanner will move the cursor to the next word. This needs to be dealt with considering your nested loop.

while(in.hasNextInt()){
word = in.next();
    while(in.hasNextInt(){
        if(word.equals(in.next()){

Good luck. Please vote my posts if you think I am being helpful, and also mark this thread as solved if your issue is solved. Peer reveiw of my suggestions is welcomed.

you should know the right way to do it. In catching, you should use catch

(Exception e){System.out.print(e.printStackTrace());}

Not quite. More like this;

(Exception e){e.printStackTrace();}

Member Avatar for joankim

Oh yeah. What you wrote or (Exception e){System.out.print("Error: " + e.getMessage());}
Mixed those up.

System.out.print(

That is not good enough for debugging. You need the whole trace, not just the message text.

Member Avatar for joankim

Never said it was for debugging. It is still a method that works for the intented purpose.

The problem is it hides where the error occurred. Without the stack trace it can be hard to locate the error.

Member Avatar for joankim

True. When I use getMessage, it's usually when I expect an error from a specific line, or when the program is very small.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.