Having trouble reading chars from text file (see post #13)

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4459 Views - Last Post: 28 January 2009 - 09:16 AM Rate Topic: -----

#1 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Having trouble reading chars from text file (see post #13)

Post icon  Posted 28 January 2009 - 05:24 AM

I'm testing out some code to read from a text file. Everything else seems to compile fine but nextChar is not being recognised.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Example4_4 {

	private void doInputs(final String fileName) {

		final FileInput in = new FileInput(fileName);
		Scanner in2 = in.getFileInputScannerObject();
		
		final int n = in2.nextInt();
		System.out.println("Integer was: " + n);
		final long l = in2.nextLong();
		System.out.println("Long was: " + l);
		final float f = in2.nextFloat();
		System.out.println("Float was: " + f);
		final double d = in2.nextDouble();
		System.out.println("Double was: " + d);
		
		final char c = in2.nextChar();
		System.out.println("Char was: " + c);
		
		final String s = in2.nextLine();
		System.out.println("Line was: " + n);
		in2.close();
	}
	
	public static void main(final String[] args) {
		Example4_4 object = new Example4_4();
		object.doInputs("test_data.txt");
	}
}




error message:
cannot find symbol - method nextChar()

This post has been edited by litterbug_kid: 28 January 2009 - 06:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble reading chars from text file (see post #13)

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:28 AM

there's no nextChar() method in the Scanner class, if you wanna read a char then
char c = sc.nextLine().charAt(0);

This post has been edited by mostyfriedman: 28 January 2009 - 05:28 AM

Was This Post Helpful? 1
  • +
  • -

#3 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:38 AM

It's still giving me the same error :/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Example4_4b {

	private void doInputs(final String fileName) {
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		System.out.println("Integer was: " + n);
		final long l = sc.nextLong();
		System.out.println("Long was: " + l);
		final float f = sc.nextFloat();
		System.out.println("Float was: " + f);
		final double d = sc.nextDouble();
		System.out.println("Double was: " + d);
		
		final char c = sc.nextChar().charAt(0);
		System.out.println("Char was: " + c);
		
		
		final String s = sc.nextLine();
		System.out.println("Line was: " + n);
		sc.close();
	}
	
	public static void main(final String[] args) {
		Example4_4b object = new Example4_4b();
		object.doInputs("test_data.txt");
	}
}


This post has been edited by litterbug_kid: 28 January 2009 - 05:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:40 AM

final char c = sc.nextChar().charAt(0);;


you got 2 semicolons there, remove one

what error are you getting now?
Was This Post Helpful? 0
  • +
  • -

#5 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:41 AM

View Postmostyfriedman, on 28 Jan, 2009 - 12:40 PM, said:

final char c = sc.nextChar().charAt(0);;


you got 2 semicolons there, remove one

what error are you getting now?


I saw that and edited my post, and re-compiled. Didn't make a difference
Same error
"cannot find symbol - method nextChar()"

This post has been edited by litterbug_kid: 28 January 2009 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:41 AM

post the error you are receiving
Was This Post Helpful? 0
  • +
  • -

#7 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:43 AM

View Postmostyfriedman, on 28 Jan, 2009 - 12:41 PM, said:

post the error you are receiving


Just did, edited my post above yours
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:43 AM

then what's the problem now?
Was This Post Helpful? 0
  • +
  • -

#9 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:45 AM

View Postmostyfriedman, on 28 Jan, 2009 - 12:43 PM, said:

then what's the problem now?

It's still not recognising method nextChar
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:47 AM

that's because it doesnt exit in the Scanner class, remove nextChar() from your code if its still there..if you want to input a character then
char c = sc.nextLine().charAt(0);
Was This Post Helpful? 0
  • +
  • -

#11 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:48 AM

View Postmostyfriedman, on 28 Jan, 2009 - 12:47 PM, said:

that's because it doesnt exit in the Scanner class, remove nextChar() from your code if its still there..if you want to input a character then
char c = sc.nextLine().charAt(0);


Oh! whoops.. sorry xD. My eyes didn't register you changing it to nextLine.
Thankyouu.. it works now
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 05:49 AM

no problem ;)
Was This Post Helpful? 0
  • +
  • -

#13 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 06:37 AM

I'm having trouble running this code:

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

class question1_11 {

	public void showFile() {
		String filename = getFileName();
		displayFileContent(filename);
	}
	
	private String getFileName() {
		Scanner sc = new Scanner(System.in);
		//Input sc = new Input();
		System.out.print("Enter filename: ");
		String filename = sc.nextLine();
		return filename;
	}
	
	private void displayFileContent(String filename) {
		Scanner sc = new Scanner(System.in);
					  
		int counter = 0;

		while (sc.hasNextLine()) {
			char s = sc.nextLine().charAt(counter);
			System.out.println(s + " ");
			counter++;
		}

		sc.close();
	}

	public static void main(String[] args) {
		new question1_11().showFile();
		
	}
	
}



error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at question1_11.displayFileContent(Main.java:27)
at question1_11.showFile(Main.java:10)
at question1_11.main(Main.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 16 seconds)
Was This Post Helpful? 0
  • +
  • -

#14 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 08:05 AM

What you are doing is reading in each line of the file and taking the appropriate character corresponding to the counter flag.

ie. First line you get first character, second line the second character, and so on. So when you evenetually have 500 or so lines, if this lines is less than that characters you thus get the index out of bounds exception.

What do you want it to do?

Print each line, certain characters? Give explanation as the output you want.

This post has been edited by BigAnt: 28 January 2009 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

#15 litterbug_kid  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 29-January 08

Re: Having trouble reading chars from text file (see post #13)

Posted 28 January 2009 - 08:26 AM

View PostBigAnt, on 28 Jan, 2009 - 03:05 PM, said:

What you are doing is reading in each line of the file and taking the appropriate character corresponding to the counter flag.

ie. First line you get first character, second line the second character, and so on. So when you evenetually have 500 or so lines, if this lines is less than that characters you thus get the index out of bounds exception.

What do you want it to do?

Print each line, certain characters? Give explanation as the output you want.


I'm trying to read in each character of the file seperately and output them

This post has been edited by litterbug_kid: 28 January 2009 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2