Results 1 to 7 of 7
- 11-02-2010, 07:53 PM #1
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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.
The errors i get here are the following:Java Code:roomnr[roomNrInput]=("Name:"+roomDataName",phone number:"+roomDataPhone); // here is the error!
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
- 11-02-2010, 08:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 11-03-2010, 07:50 AM #3
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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
- 11-03-2010, 07:56 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Assuming that 'roomnr' is an array of Strings the following line should result in a String:
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.Java Code:("Name:"+ roomDataName ",phone number:"+ roomDataPhone)
kind regards,
Jos
- 11-03-2010, 02:03 PM #5
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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
- 11-03-2010, 02:07 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-03-2010, 02:33 PM #7
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
Similar Threads
-
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
data is not inserting into database
By gb.rashu in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 08-21-2010, 03:05 PM -
Problem regarding Inserting data into Mysql via JSP
By abhi118 in forum JDBCReplies: 5Last Post: 04-05-2010, 01:24 PM -
Inserting data to database from the form
By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-17-2008, 01:06 AM -
Inserting data containing quotes into DB
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks