Hi there folks. I have an app that has several classes but for some reason one of these classes isn't giving me a return value. What I want to check is whether or not this code will return the expected string...

String seatLayout;

public String showAllSeats(String flightNumber){
		
		fn = flightNumber;
		
	// Make a set of arrays for each flight. This is only one flight. If the flightNumber is "1294"  ... 

		if(fn == "1294"){

			ArrayList<String> r1 = new ArrayList<String>(); 
			r1.add("Row 1");
			r1.add("_");
			r1.add("_");
			r1.add("_");
			String row1 = r1.toString();
			
			ArrayList<String> r2 = new ArrayList<String>();
			r2.add("Row 2");
			r2.add("_");
			r2.add("_");
			r2.add("_");
			String row2 = r2.toString();
			
			ArrayList<String> r3 = new ArrayList<String>();
			r3.add("Row 3");
			r3.add("_");
			r3.add("_");
			r3.add("_");
			String row3 = r3.toString();
			
			seatLayout = row1 + row2 + row3;
			
					
		}
		return seatLayout;

Is > String row1 = r1.toString(); < OK? I'm trying to convert the String array to a simple string. I think it's OK (Eclipse doesn't complain.)

If thats OK then > seatLayout = row1 + row2 + row3; < should be OK.

For some reason seatLayout isn't getting returned to main(). The code in main() that calls the method above is...

Seats s = new Seats();                    //Create Seats instance
int flightNum = fl1.getFlightNum();    //Get the flight number (from Flight class)

String flightN = Integer.toString(flightNum);  //Must convert flight number int to String first

System.out.print(s.showAllSeats(flightN));  // Pass flight number and Print the return

seatLayout should get returned to main() in the last line above (the print statement) but it isn't. I've tried fooling around with variable scope, etc. I suspect that the ArrayList code above isn't even running, but I'm passing the correct flight number to showAllSeats().

Any advice is greatly appreciated! Many Thanks!

...Tyster

Recommended Answers

All 3 Replies

This managed to work for me--

import java.util.*;

public class TestClass1{
	static String seatLayout = null;
	static String fn = null;

	public static void main(String... args){
		System.out.println(showAllSeats("1294"));
	}

	public static String showAllSeats(String flightNumber){

		fn = flightNumber;

		// Make a set of arrays for each flight. This is only one flight. If the flightNumber is "1294"  ...
		if(fn.equalsIgnoreCase("1294")){

			ArrayList<String> r1 = new ArrayList<String>();
			r1.add("Row 1");
			r1.add("_");
			r1.add("_");
			r1.add("_");
			String row1 = r1.toString();

			ArrayList<String> r2 = new ArrayList<String>();
			r2.add("Row 2");
			r2.add("_");
			r2.add("_");
			r2.add("_");
			String row2 = r2.toString();

			ArrayList<String> r3 = new ArrayList<String>();
			r3.add("Row 3");
			r3.add("_");
			r3.add("_");
			r3.add("_");
			String row3 = r3.toString();

			seatLayout = row1 + row2 + row3;
		}
		return seatLayout;
	}
}
commented: Thanks for your help! +2

in showAllSeats, I'd change
fn == "1294"
to
fn.equals ("1294")

the double equals checks for object equality (i.e. the same object in memory)
.equals() checks to see if it's the same string

commented: Thanks for your help! +2

Hey Brian and Alex...

Many Thanks, guys. I had a couple of mistakes, but using == instead of .equals was the big one. I'm getting the expected return now.

Many Thanks to both of you!!

...Ty

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.