Results 1 to 7 of 7
  1. #1
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default Problems with printing variables and inserting data into array

    Hey fellow java-coders.

    I was hoping some of you could help me out with the following problem i have.
    I'm working on a system that hotels could use to book their rooms and check room numbers and so on.(Non commercial system, just hobby).

    I am encountering some problems in the part where i ask the room number future-guests will be in. Where after you can add some data to this room number such as name, phone number or credit card data. I have created a String array(roomnr) that will held this data.

    When i try to add data in to this array i get a load of errors.
    Java Code:
    roomnr[roomNrInput]=("Name:"+roomDataName",phone number:"+roomDataPhone); // here is the error!
    The errors i get here are the following:
    cannot find symbol roomnr
    ')' expected.
    not a statement
    ';' expected.

    roomNrInput is an Integer i've already asked earlier. E.g if they entered room number 5. I hope in this will it will add data into roomnr[5]

    The complete code i have highlighted important lines:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package homage;
    import java.io.*;
    import java.util.Scanner;
    /**
     *
     * @author Admin
     */
    public class Main {
    
    
        public static void main(String[] args) { // array that is going to hold all data such as who is currently in which room
    [B]                String[] roomnr;
            roomnr = new String[10];
            roomnr[1]="";
            roomnr[2]="";
            roomnr[3]="";
            roomnr[4]="";
            roomnr[5]="";
            roomnr[6]="";
            roomnr[7]="";
            roomnr[8]="";
            roomnr[9]="";
            roomnr[10]="";[/B]
    
        bookRooms();
        }
    
        public static String bookRooms () { //main menu
        System.out.println("1.Book a room");
        System.out.println("2.Check roomstatus");
        Scanner bookRoomsSC=new Scanner(System.in);
        int bookRoomsSc;
        bookRoomsSc = bookRoomsSC.nextInt(); // Scanner for the roombooking menu.
    
        if (bookRoomsSc==1) { // from here starts the code that adds new data to a roomnumber.
            int roomNrInput;
            Scanner askingforroomnumber=new Scanner(System.in); // asking the room number we are going to add data to.
            System.out.println("Which room are you going to book?");
            roomNrInput=askingforroomnumber.nextInt();
    
            BufferedReader roomdata = new BufferedReader(new InputStreamReader(System.in));
    
                    try {
                        String roomDataName;
                        System.out.println("Booker's name:"); // asking for the booker's name.
                        roomDataName=roomdata.readLine();
    
                        String roomDataPhone;
                        System.out.println("Booker's phonenumber?"); // asking for the bookeer's phone number.
                        roomDataPhone=roomdata.readLine();
    [B]                    roomnr[roomNrInput]=("Name:"+roomDataName",phone number:"+roomDataPhone); // here is the error! [/B]
                    }
    
                    catch(IOException ioe) {
                        System.out.println("IOException occured!");
                    }
        }
        else if(bookRoomsSc==2) {
            //code still to produce
        }
        else {
            System.out.println("Please make your choice.");
        }
    
    
        return "Went back to main";
    }
    
    }
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    When i try to add data in to this array i get a load of errors.
    Java Code:
    roomnr[roomNrInput]=("Name:"+roomDataName",phone number:"+roomDataPhone); // here is the error!
    The expression does not evaluate to a String; check the quotes and plus signs.

    kind regards,

    Jos

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    I've searched over the internet and everyone seems to be printing variables the same way as i do.

    I see that this line should be a string but roomDataName and RoomDataPhone are strings.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    I've searched over the internet and everyone seems to be printing variables the same way as i do.

    I see that this line should be a string but roomDataName and RoomDataPhone are strings.
    Assuming that 'roomnr' is an array of Strings the following line should result in a String:

    Java Code:
     
    ("Name:"+roomDataName",phone number:"+roomDataPhone)
    The expression should be of the form x+y+z+ ...+t where x, y, z and t are Strings. Let's break up your expression:

    Java Code:
    ("Name:"+
    roomDataName
    ",phone number:"+
    roomDataPhone)
    See? There's a + missing at the end of line 2 and 4 so it is not a valid expression.

    kind regards,

    Jos

  5. #5
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    Thanks that helped a lot,
    ehmm one small question i have left. the array roomnr is in the main method. But im trying to acces is it from the BookRooms method. All variables that are in the main method can be acces in other methods right?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by coltragon View Post
    Thanks that helped a lot,
    ehmm one small question i have left. the array roomnr is in the main method. But im trying to acces is it from the BookRooms method. All variables that are in the main method can be acces in other methods right?
    Nope, all variables defined in a method, any method are local to that method unless you pass them as a parameter to another method. Methods defined in a class are visible in the entire object and static variables are visible in the entire class and all objects (instantiations) of that class. There is nothing special about the main( ... ) method, it is just a public static method.

    kind regards,

    Jos

  7. #7
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    Hehe i was just coming back to this thread to say that i found out by myself. But thanks for your help anyways. It is appreciated and others who come here by google will find it useful :D
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  2. data is not inserting into database
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 08-21-2010, 04:05 PM
  3. Replies: 5
    Last Post: 04-05-2010, 02:24 PM
  4. Inserting data to database from the form
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 02:06 AM
  5. Inserting data containing quotes into DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 10:28 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
  •