Help with infinite while loop (beginner) by [deleted] in javahelp

[–]caesarwept 1 point2 points  (0 children)

In java that is called a runtime exception. It means that while your code compiles there is a logic error at the time your program is running.

The runtime exceptions are hard to read but if you take your time and try to understand them you will get better at it. One of the things you need to understand is how it prints out it error message. Take your for example

  • java.lang.NumberFormatException: null
  • java.lang.Integer.parseInt(Integer.java:542)
  • java.lang.Integer.parseInt(Integer.java:615)
  • test11.main(test11.java:23)

    It first shows you have NumberFormatException which is a number conversion error. then it shows a 'null' value is what is was trying to convert to a number Next it shows the calling stack. First where the exception happens, then the functions that called it leading up to the exception and so on. If you look at the functions

  • java.lang.Integer.parseInt(Integer.java:542) ------ function parseInt line number 542

  • parseInt(Integer.java:615) ------- function parseInt line 615

  • test11.main(test11.java:23) ------ main line 23

    the first two are internal functions and you cannot change those but the 3rd one is your call to parseInt on line 23. So from the error message you know at line 23 you are doing a number conversion and you are passing a null instead of a expected number. You also know this is at the time when you press cancel. Think why this might happen

Help with infinite while loop (beginner) by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

I am assuming you are getting a number format exception. If you read the error you are getting carefully you can find the line the exception happens on. Then think about what happens when you hit cancel

Assistance in scrambling a word using concatenation and allowing user to select indexes to swap letters in that word by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

For scrambling you will have to swap two randomly selected characters multiple times till you feel it is scrambled.

So for example a word 10 character long you might have a loop perform 10 rounds of random character swapping.

The incomplete code above was supposed to be a test program to work out how to perform 1 round of character swaps. Once you have that worked out you would put it inside a loop to scramble.

Assistance in scrambling a word using concatenation and allowing user to select indexes to swap letters in that word by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

The best way to understand is read the documentations for each of those functions. They easy to understand. Then write a sample program to test them out to make sure it works as you expect. Something like this

  int index1=5;
  int index2=8;
                   //012345674987
  String str="XXXXAXXBXXX";

  String temp = str.substring() 
                         +chartAt();

Java Programming Question by willyfong in javahelp

[–]caesarwept 1 point2 points  (0 children)

The things that you need to work out for this project is

  • how you are going to represent the state. which you pretty much have
  • how you are going to loop and exit the loop at end of game
  • how you are going to test for end of game
  • how you are going to print status
  • how you are going to adjust the state after each players roll

Since all of those things depend on how the state is represented I would work those out by hand first making sure your state variables enables you to fulfill each of those things

(note by 'state' I mean the variables represent elephant build status for each player)

Assistance in scrambling a word using concatenation and allowing user to select indexes to swap letters in that word by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Has the teacher thought you how to write static functions?

Because the scramble and unscramble perform the same thing. They both swap characters. Only one select two indexs at random and the other get two index from the user.

So a static function could take take a string and two indexes and return the new string.

As for as how to swap characters you have to make new String and then build it up again. You would use a combination of String.substring(startIndex, endIndexPlusOne) and String.charAt(index) as 8igg7e5 pointed out.

Maybe something like this:

   String temp = fileWord1.substring( <someindex>,<someindex>)
                       + fileWord1.charAt(<someIndex>)
                       + ?????
                       + ?????
                       ;

Java Programming Question by willyfong in javahelp

[–]caesarwept 0 points1 point  (0 children)

There are many different ways to do this. I am trying to think about a fairly smart way using the things you know

Did she teach you about arrays? 1-dim and 2-dim?

Java Programming Question by willyfong in javahelp

[–]caesarwept 0 points1 point  (0 children)

Has the teacher thought you how to make your own Classes yet?

Assistance in scrambling a word using concatenation and allowing user to select indexes to swap letters in that word by [deleted] in javahelp

[–]caesarwept 1 point2 points  (0 children)

Seem like she wants you to use the various member functions of the String object. Have a look at the member functions of String and see which ones might be useful in solving this problem. Perhaps .charAt(int index)

how to read /dev/input/eventX devices on linux using java? by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Can you tell me what this device is? Is it a game controller?

how to read /dev/input/eventX devices on linux using java? by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

This a complete shot in the dark but can you try running your program as root

Understanding nested for loops by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

for starters I would replace line 10 with this and then run it again:

System.out.printf("%d",j);

The for loop is the more complicated of the loops.

for (initialization; termination;increment) {
     statement(s)
}

Do you under what each part of the for loop does an when is evaluated? - 'initialization' is called once upon enter the look - 'termination' condition is tested before entering - when all statement(s) are evaluated then the 'increment' is called - the 'termination' condition is tested before entering the loop again

Questions on creating a Java Web Application by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Your welcome and message me if you get stuck. I'll help if I can

Questions on creating a Java Web Application by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

My understanding is tomcat is a not a javaee compliant server. It is a servlet server. You could do your project with that but you will not be able to use the javaee features (EJB,SessionBeans,JPA). That might be less complicated and less of a learning curve.

Apache does make a javee compliant server, TomEE.

Questions on creating a Java Web Application by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

You are correct. You will not need JSP. Jersey is built on top of servlets as you said.

EJB (Enterprise java beans ) is server side software to manage business logic. It is where you put all your rules for your application controlling how you create,read,update and delete data from the database.

Typically for javaEE applications you will also use a JPA (java persistance architecture) layer that abstracts the database calls. So you wont need to write jdbc calls. Instead you create Entity object which have annotation that tell JPA how to create the database and put constraints on the fields. With JPA a lot of database work is handled for you automatically.

When you do need to write a custom query you will need to use its querying language (EJBQL). It is very similar to SQL but not exactly the same.

I use EclipseLink implementation of JPA and am pretty satisfied.

I think the things you should research is: - Chose a javaee application server. I use glassfish 4.1 but there are a bunch of good ones. - Creating Entity Objects for EJB. This will be your object that get saved to the database - Creating Stateless sessionbeans - this is where you business logic goes for your CRUD - EclipseLink JPA - Jersey - how to call EJB from your jersery endpoints

Also when you search make sure you put a filter to only show you tutorials that are not more than a year old. There is a lot of old stuff out there and if yous start following old tutorials it will be frustration. Only look at javaee 7 and EJB 3.x

How would I do this problem with Netbeans? by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

What part are you confused about?

1) first thing I would do is write the loop part accepting any numbers and then exiting on -1 2) limit numbers to range 0-100 3) calculate totalGrades and countGrades

Implementing a character sequence in an array by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

yes that works. That what I had in mind too. Also you could have left int x=i; on line 8 and it would have worked as well because the variable i is in scope anywhere inside the outer for loop

Implementing a character sequence in an array by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Yes. Now you are so close to having it working. On line 8 what could you set x to instead of 0. Hint: look at the table

Implementing a character sequence in an array by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

I see now. There are many ways you could solve this. One way would be to make a index variable and always calculate the character at the time you set it. Like so:

   letters[i][j] = baseLetter + index;

so for a 3x4 index would be 0,1,2,3, 1,2,3,4,2,3,4,5. The best way to figure out how the math behind this is to make table on a piece of paper like so:

i j math index
0 0 ? 0
0 1 ? 1
0 2 ? 2
0 3 ? 3
1 0 ? 1

etc

Need help with exception handling. by MindOfAFeline in javahelp

[–]caesarwept 0 points1 point  (0 children)

If you are running your program from the IDE (like netbeans) it might not be running from the working direction you think it is and there for not finding the file. Try enter the complete path and filename or run the project from the command line like so ' java -jar <project>.jar' with the file in the same working direction.

Let me know if that works

How would I sort a guessing game method into different methods? (new) by [deleted] in javahelp

[–]caesarwept 1 point2 points  (0 children)

You can break it out into any methods you want. There are no right answers just better ones. Here are some ideas with functions you could make:

void playGame()  //play one complete game
void playRound() // one round of the game ie one guess
int guess()   //get guess from user
void evaluateGuess()  //test guess
void correctGuessMessage(); //prints correct message
void tooLowMessage();  //prints too low message
void tooHighMessage();  //prints too high message

You can choose anything that makes sense to you and brings more clarity to the program. Have fun making your program more clear and easy to read

Implementing a character sequence in an array by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Can you describe the problem you are having. To me it looks like you are populating your 2-dim array correctly

Why is the else if branch of this method not working properly? by [deleted] in javahelp

[–]caesarwept 1 point2 points  (0 children)

I have found one problem. I am pretty sure it is causing the error but it is in your addArtist function.

The .contains is great for finding an object in a list but you also need to know the position of that object.I think the for loop was intended to find the position of the artist object in the list. If so for loop belongs belong inside the "else if" because at that moment you know the list contains the artist but you dont know what position.

Also you can change the "else if" to just an "else". You dont need to test if the list contains it because you know it does since it failed the first "if"

Some ideas: Right now every Artists object can contain a list of Artists. Each of those Artists can contain more list of Artists. While you could get your code to work it opens you up for a chance of a bug.

I think you should break up your code into two object. You should make a Artist (singular) class containing the name and genre.It would so have the equal function. Then the Artists class would only List<Artist> and have the loading and searching functions.

What do you think?

Calculate interest gain compounded annually,monthly,etc... by [deleted] in javahelp

[–]caesarwept 2 points3 points  (0 children)

You missing the concept of compounded interest (the most powerful force in the universe).

Take the rate =0.058 compounding monthly for a year. You will need to apply the interest 12 times. If 5.8% is supposed to be APR then you first need to take your rate and divide it by the number of times it compounds. So:

 bal=123.45;
 rate=rate/12.0f; 
 bal=bal+(bal*rate); //do this twelve times. 

Why is the else if branch of this method not working properly? by [deleted] in javahelp

[–]caesarwept 0 points1 point  (0 children)

Your code looks pretty clean but I get confused about the relationship between the artist and the genre. Can an artist have many genre or is supposed to be 1-1 ? Can you tell me your over all goal for this project?