Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

C# Reading individual data from files similar to Java

Recommended Posts

Posted (edited)

I've recently begun to learn C#. As you can see from this code snippet: 

Scanner file = new Scanner(new File("hashValues.txt"));
		
String miningDay = "";
String currency = "";
double netPayOut = 0.0;
String i = "";
String walletID = "";
Array hashValues;
while(file.hasNext())
{
	miningDay = file.next();
	currency = file.next();
	netPayOut = file.nextDouble();
	i = file.next();
	walletID = file.next();
}

Java has a class called Scanner which allows you to open a file through the file class. The Scanner class has functions to read individual strings, as well as int and double data types. Does C# have something similar to this? I tried to do something like this, but it doesn't really work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;

namespace Mod3_Lab1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader addressBook = null;

            String Name = "";
            String Address = "";
            try
            {
                addressBook = new StreamReader("address_book.txt");

                while(!addressBook.EndOfStream)
                {
                    Name = addressBook.ReadLine();
                    Address = addressBook.ReadLine();
                }

                addressBook.Close();

                Console.WriteLine("Name: " + Name);
                Console.WriteLine("Address: " + Address);
            }
            catch(FileNotFoundException)
            { 

            }
        }
    }
}

Here is the output: 

a17ea39570.png

Edited by Enerjak

Share this post


Link to post
Share on other sites
Advertisement

are you sure the name and address are on separate lines?

 Can you show us the contents of address_book.txt?

Share this post


Link to post
Share on other sites
Posted (edited)
18 minutes ago, ChaosEngine said:

are you sure the name and address are on separate lines?

 Can you show us the contents of address_book.txt?

No, Name and address are not on separate lines. 

Quote

Robert Holidy 401 bridge Avenue

However, the code I wrote here:

package Def;

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

import javax.swing.JOptionPane;
public class Application 
{
	public void main(String[] ages)
	{
		
		try
		{
			Scanner scan = new Scanner(new File("Address_book.txt"));
			String Name = "";
			String Address = "";
		
			while(scan.hasNext())
			{
				Name = scan.next();
				Address = scan.next();
			}
			
			JOptionPane.showMessageDialog(null, Name);
			scan.close();
		}
		catch(FileNotFoundException e)
		{
			
		}
	}
}

This should load Address_book.txt - which for some reason it doesn't, but that's another problem all together. 

Edited by Enerjak

Share this post


Link to post
Share on other sites
6 hours ago, Enerjak said:

No, Name and address are not on separate lines

Ok, well your code is expecting them on separate lines.

// reads one line
Name = addressBook.ReadLine();
// reads next line
Address = addressBook.ReadLine();

if you change address_book.txt to something like 

Quote

Robert Holidy

401 bridge Avenue

1

it will work.

The other problem is that if you have more than one line, you will only store the last line in the Name and Address strings.

If you want to put them on one line, you will need some kind of delimiter (a tab character, or a | ) to say when the name ends and the address begins (you could just assume that the name is the first two words, but then you're stuffed if someone puts a middle name or initial).

I don't know much about the Scanner class, but a cursory google tells me your java code won't work either.

Share this post


Link to post
Share on other sites
2 hours ago, ChaosEngine said:

Ok, well your code is expecting them on separate lines.


// reads one line
Name = addressBook.ReadLine();
// reads next line
Address = addressBook.ReadLine();

if you change address_book.txt to something like 

it will work.

The other problem is that if you have more than one line, you will only store the last line in the Name and Address strings.

If you want to put them on one line, you will need some kind of delimiter (a tab character, or a | ) to say when the name ends and the address begins (you could just assume that the name is the first two words, but then you're stuffed if someone puts a middle name or initial).

I don't know much about the Scanner class, but a cursory google tells me your java code won't work either.

I got the code in Java to work. Here is the fixed code:

package address_book;

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

public class application 
{
	public static void main(String[] args)
	{
		Scanner file = null;
		person per = null;
		try
		{
			file = new Scanner(new File("address_book.txt"));
			
			String firstName = "";
			String lastName = "";
			String address = "";
			
			while(file.hasNext())
			{
				firstName = file.next();
				lastName = file.next();
				address = file.nextLine();
				per = new person(firstName, lastName, address);
			}
			file.close();
			
			System.out.println(per.getFirstName() + " " + per.getLastName() + " " + per.getAddress());
		}
		catch(FileNotFoundException e)
		{
			
		}
	}
}

This is what it outputs:

0f3cd9c5fc.png

And here is the address book file again just for reference:

Quote

Robert Holiday 500 bridge Avenue

As you can see from the fixed code above, I have made three string variables, firstName, lastName, and address. In order to get the first name from the file, I used Scanner.next(), which reads the string "Robert." Scanner.next() only reads one string until there is a space. Next I read in the last name using another Scanner.next() to get the last name. The Scanner.nextline() gets the rest of the line in the file - which is the address. 

Share this post


Link to post
Share on other sites

Again, that assumes that your name will always be two words separated by a space. What happens if someone adds John F. Kennedy? or Carrie Anne Moss?

Who is generating the address_book.txt file? If it's you, you might consider adding a delimiter.

 

 

Share this post


Link to post
Share on other sites
12 minutes ago, ChaosEngine said:

Again, that assumes that your name will always be two words separated by a space. What happens if someone adds John F. Kennedy? or Carrie Anne Moss?

Who is generating the address_book.txt file? If it's you, you might consider adding a delimiter.

 

 

I found this on Google: https://stackoverflow.com/questions/24041113/error-1-inconsistent-accessibility-return-type-is-less-accessible-than-method It shows how to split the lines into an array using a delimiter - like you said I should use. I should have searched more than make a post on it. Thanks for your help. 

Share this post


Link to post
Share on other sites
2 hours ago, Enerjak said:

I found this on Google: https://stackoverflow.com/questions/24041113/error-1-inconsistent-accessibility-return-type-is-less-accessible-than-method It shows how to split the lines into an array using a delimiter - like you said I should use. I should have searched more than make a post on it. Thanks for your help. 

Are you sure that's the right link? I don't see anything about splitting strings there. 

but yeah, basically you want the string.Split method. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!