Page 1 of 2 12 LastLast
Results 1 to 20 of 34

Thread: String equal

  1. #1
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default String equal

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMap {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    
    		Scanner scan = new Scanner(System.in); // reads user input
    		System.out.println("Press y to see a PA or !y to end program.");
    		command = scan.next(); // stores user input
    		System.out.println(command);
    
    		if (command == "y") { // user asks to print PA
    			System.out.println("Enter file's name with .txt at the end.");
    			fileName = scan.next(); // stores user input
    			scan.close();
    		} else if (command == "!y") { // user ends program
    			System.exit(0);
    		}
    
    	}
    
    
    	public static void printImage() throws FileNotFoundException {
    
    
    	}
    
    }
    for some reason the if statement is not checking the command content

    and for the printImage method, how do i go back to the main method
    Last edited by Dinomite07; 02-16-2016 at 03:06 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    You need to use the equals() method to compare the contents of objects like Strings. The == operator is mostly for primitives.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Thank you
    Last edited by Dinomite07; 02-16-2016 at 04:17 AM.

  4. #4
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMap {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    
    		Scanner scan = new Scanner(System.in); // reads user input
    		System.out.println("Press y to see a PA or !y to end program.");
    		command = scan.next(); // stores user input
    		if (command.equals("y")) { // user asks to print PA
    			System.out.println("Enter file's name with .txt at the end.");
    			fileName = scan.next(); // stores user input
    			scan.close();
    			printImage();
    		} else if (command.equals("!y")) { // user ends program
    			System.exit(0);
    		}
    
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char [][] image = new char [100][100];
    
    	public static void printImage() throws FileNotFoundException {
    
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    
    		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				image[y][x] = fileRead.next().charAt(x);
    				System.out.print(image[y][x]);
    			}
    		}
    
    		fileRead.close();
    
    	}
    
    }
    I am trying to read a text file. The first two numbers are the height and width. I need to be able to also read, store, and print the characters including the spaces.
    I can't figure out how to read the characters(including the spaces) in the text file.
    image[y][x] = fileRead.next().charAt(x);

    Text File Example

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    Also how do you loop back to the main method after printImage is done?
    Last edited by Dinomite07; 02-16-2016 at 04:25 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    how do you loop back to the main method after printImage is done?
    Have a loop (like a while loop) in the method where you want to execution to loop. If the code calls printImage at the end of the loop, when printImage returns the loop will send execution back to the top of the loop.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    Have a loop (like a while loop) in the method where you want to execution to loop. If the code calls printImage at the end of the loop, when printImage returns the loop will send execution back to the top of the loop.
    Will finish once then break.

    Console:

    Press y to see a PA or !y to end program.
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at TileMap.main(TileMap.java:15)

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMap {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    
    		while (true) {
    			command = null;
    			Scanner scan = new Scanner(System.in); // reads user input
    			System.out.println("Press y to see a PA or !y to end program.");
    			command = scan.next(); // stores user input
    			if (command.equals("y")) { // user asks to print PA
    				System.out.println("Enter file's name with .txt at the end.");
    				fileName = scan.next(); // stores user input
    				scan.close();
    				printImage();
    			} else if (command.equals("!y")) { // user ends program
    				System.exit(0);
    			}
    		}
    
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char[][] image = new char[100][100];
    
    	public static void printImage() throws FileNotFoundException {
    
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    
    		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				// image[y][x] = fileRead.next().charAt(x);
    				// image[y][x] = fileRead.nextLine().charAt(0);
    				// System.out.print(image[y][x]);
    			}
    		}
    
    		fileRead.close();
    
    	}
    
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: String equal

    I'm guessing this is not happening on the first run through your loop.
    You should not close the Scanner (line 19) as that closes the System.in stream, which is rarely a good idea.
    It's fine for file reading, but not when using the Sys.in.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Fixed, thank you.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMap {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    
    		while (true) {
    			command = null;
    			Scanner scan = new Scanner(System.in); // reads user input
    			System.out.println("Press y to see a PA or !y to end program.");
    			command = scan.next(); // stores user input
    			if (command.equals("y")) { // user asks to print PA
    				System.out.println("Enter file's name with .txt at the end.");
    				fileName = scan.next(); // stores user input
    				printImage();
    			} else if (command.equals("!y")) { // user ends program
    				System.exit(0);
    			}
    		}
    
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char[][] image = new char[100][100];
    
    	public static void printImage() throws FileNotFoundException {
    
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    
    		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				// image[y][x] = fileRead.next().charAt(x);
    				// image[y][x] = fileRead.nextLine().charAt(0);
    				System.out.print(image[y][x]);
    			}
    		}
    
    		fileRead.close();
    
    	}
    
    }
    I am trying to read a text file. The first two numbers are the height and width. I need to be able to also read, store, and print the characters including the spaces. I know next() skips the spaces so I need help figuring out what functions to use to scan the file.


    Text file

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    If you want to preserve what is on a line, use the nextLine() method.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    If you want to preserve what is on a line, use the nextLine() method.
    Text file

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    image[y][x] = fileRead.nextLine().charAt(x);

    This command is suppose to read each character on each line including spaces and store and print them.

    I have no idea what this error means?

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at TileMap.printImage(TileMap.java:40)
    at TileMap.main(TileMap.java:19)

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMap {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    
    		while (true) {
    			command = null;
    			Scanner scan = new Scanner(System.in); // reads user input
    			System.out.println("Press y to see a PA or !y to end program.");
    			command = scan.next(); // stores user input
    			if (command.equals("y")) { // user asks to print PA
    				System.out.println("Enter file's name with .txt at the end.");
    				fileName = scan.next(); // stores user input
    				printImage();
    			} else if (command.equals("!y")) { // user ends program
    				System.exit(0);
    			}
    		}
    
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char[][] image = new char[100][100];
    
    	public static void printImage() throws FileNotFoundException {
    
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    
    		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				image[y][x] = fileRead.nextLine().charAt(x);
    				System.out.print(image[y][x]);
    			}
    		}
    
    		fileRead.close();
    
    	}
    
    }

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    I have no idea what this error means?

    java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    The OutOfBounds part means that an index was used that was past the end of the String.
    The 0 (the value of the bad index) means that the String was empty. It did not have a char at index 0.

    at java.lang.String.charAt(Unknown Source)
    at TileMap.printImage(TileMap.java:40)
    That says the statement at line 40 called the charAt() method,
    with an index of 0 on an empty String.

    Why does the code use the nextLine() method and only look at one char that was read? The rest of the text on the line will be lost because the String returned by the nextLine() was not assigned to a variable.

    The reason the String is empty is because of the way nextLine() works.
    Take a look at this: https://christprogramming.wordpress....on-mistakes-1/
    Last edited by Norm; 02-17-2016 at 12:23 AM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    So how do I read more than one character, return the value to the array and then go to the next line?

    test.txt

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMapTest {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char[][] image = new char[100][100];
    
    	public static void printImage() throws FileNotFoundException {
                    fileName = "test.txt";
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    
    		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				image[y][x] = fileRead.nextLine(); //what do I do?
    				System.out.print(image[y][x]);
    			}
    		}
    
    		fileRead.close();
    
    	}
    
    }
    Last edited by Dinomite07; 02-17-2016 at 01:55 AM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    how do I read more than one character,
    The nextLine() method reads a whole line of text up to the end of line character.

    What is wrong with the code in post#12?
    Last edited by Norm; 02-17-2016 at 02:51 AM. Reason: Changed #11 to #12
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    The nextLine() method reads a whole line of text up to the end of line character.

    What is wrong with the code in post#11?
    So image[y][x] = fileRead.nextLine().charAt(0); reads the first character of the first line?


    What code in post 11? The website?

    Console prints out nothing. It should be printing out a "a".

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMapTest {
    
    	private static String command = null;
    	private static String fileName = null;
    
    	public static void main(String args[]) throws FileNotFoundException {
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char[][] image = new char[100][100];
    
    	public static void printImage() throws FileNotFoundException {
    		fileName = "test.txt";
    		Scanner fileRead = new Scanner(new File(fileName)); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    		char image1;
    		image1 = fileRead.nextLine().charAt(0);
    		image1 = fileRead.nextLine().charAt(0);
    		System.out.print(image1);
    /*		for (int y = 0; y < imageHeight; y++) { // reads and prints text file
    			System.out.println("");
    			for (int x = 0; x < imageWidth; x++) {
    				image[y][x] = fileRead.nextLine().charAt(0);
    				System.out.print(image[y][x]);
    			}
    		}
    */
    		fileRead.close();
    
    	}
    
    }
    Last edited by Dinomite07; 02-17-2016 at 02:24 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    Now the code on line 22 reads a line and discards it
    then the code on line 23 reads the next line, gets the first letter of the line and stores it in the variable: image1.

    What char values are supposed to be stored in the 100x100 array named image?
    Last edited by Norm; 02-17-2016 at 03:07 AM. Reason: changed variable to image1
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    Now the code on line 22 reads a line and discards it
    then the code on line 23 reads the next line, gets the first letter of the line and stores it in the variable: image.

    That code should not compile because image is an array and you need array notation to access an element in it like the code on line 25 in post#12

    What char values are supposed to be stored in the 100x100 array named image?
    The 100x100 array is going to hold characters of an ascii text art txt file.

    I am just trying to read a character, store the character and print the character out. Once I figure that out I am going to use an array.

    lines 21 to 24 use char image1 and not the array image

    the console should have printed out "a" but it is blank

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    The 100x100 array is going to hold characters of an ascii text art txt file.
    10,000 characters? Why 100x100? How does that size relate to the size of the txt file? 100 lines with 100 char each?

    When printing out variables for debugging add delimiting Strings to make it easier to see what the variable contains. It is hard to see a single space:
    Java Code:
       System.out.print(">"+image1+"<");
    Java Code:
     image1 = fileRead.nextLine().charAt(0); //  reads line and saves 1st char
    If the line has 100 char, the above statement only saves the first character, the rest of the line is lost.
    Last edited by Norm; 02-17-2016 at 03:15 AM.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    10,000 characters? Why 100x100? How does that size relate to the size of the txt file? 100 lines with 100 char each?

    When printing out variables for debugging add delimiting Strings to make it easier to see what the variable contains. It is hard to see a single space:
    Java Code:
       System.out.print(">"+image1+"<");
    Java Code:
     image1 = fileRead.nextLine().charAt(0); //  reads line and saves 1st char
    If the line has 100 char, the above statement only saves the first character, the rest of the line is lost.

    There is a char at index [0] "a".

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at TileMapTest.printImage(TileMapTest.java:18)
    at TileMapTest.main(TileMapTest.java:7)

    Why isn't the code working?

    test.txt

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class TileMapTest {
    
    	public static void main(String args[]) throws FileNotFoundException {
    		printImage();
    	}
    
    	private static int imageHeight = 0;
    	private static int imageWidth = 0;
    	private static char image1;
    	
    	public static void printImage() throws FileNotFoundException {
    		Scanner fileRead = new Scanner(new File("test.txt")); // reads from file
    		imageHeight = fileRead.nextInt(); // reads height
    		imageWidth = fileRead.nextInt(); // reads width
    		image1 = fileRead.nextLine().charAt(0);
    		System.out.print(">"+image1+"<");
    		fileRead.close();
    	}
    }
    Last edited by Dinomite07; 02-17-2016 at 04:25 AM.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: String equal

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at TileMapTest.printImage(TileMapTest.java:18)
    That looks like the same problem from post#10. See post#11.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: String equal

    Quote Originally Posted by Norm View Post
    That looks like the same problem from post#10. See post#11.
    test.txt

    4 5
    aa aa
    aaa a
    a aaa
    aaa a

    This is my logic.

    imageHeight = fileRead.nextInt(); // reads height
    imageWidth = fileRead.nextInt(); // reads width

    the first 2 numbers are read and stored

    image1 = fileRead.nextLine().charAt(0);

    this command should go to the next line and scan the first character which is "a" and then store and print

    next line: aa aa

    I don't understand why there is an error. What is image1 = fileRead.nextLine().charAt(0); doing?
    What do I need to add or change to make the program work?
    Last edited by Dinomite07; 02-17-2016 at 04:52 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 07-05-2013, 06:10 PM
  2. String not equal to...... :(
    By GrumpyBum in forum New To Java
    Replies: 5
    Last Post: 04-21-2012, 11:41 PM
  3. x input string is not equal to "x"
    By monkeynote in forum New To Java
    Replies: 3
    Last Post: 01-18-2012, 08:45 AM
  4. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 10:01 PM
  5. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •