Reading individual data from files similar to Java

Started by
8 comments, last by Enerjak 6 years, 1 month ago

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

Advertisement

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

 Can you show us the contents of address_book.txt?

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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. 

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.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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. 

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.

 

 

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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. 

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. 

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
2 minutes ago, ChaosEngine said:

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. 

Whoops! Here is the right link: https://stackoverflow.com/questions/9763796/reading-from-a-text-file-and-splitting-each-individual-line-into-different-arra go all the way down. 

This topic is closed to new replies.

Advertisement