Ask a question

I Am A Newbie Needing Help With A Java Program Using If/else

JAVA program help needed?

/*This program reads in a string from the keyboard, chops the string into tokens,
* checks each character in the token to see if they are ALL digits( no 7B or 5! ).
* /

public static void main(String[] args)
{
boolean itIsAnInt= true;

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter int values separated by spaces: ");//make it user friendly
String str = keyboard.nextLine( );

Scanner sc = new Scanner(str); //So we can break the string into tokens (parts)

while( sc.hasNext( ) )
{

String temp = sc.next(); //read in a token
for(int i = 0; i < temp.length( ); i++)
{
if( !Character.isDigit(temp.charAt(i) ) //If a token contains a caharcter that is not a digit
{ System.out.println( "Error: Not an int value!");
itIsAnInt= false;
break;
}


}//end of for-loop

if( itIsAnInt ) //The token had all digits so it is an int
{
System.out.println("The Int is:" + Integer.valueOf(temp);

}

}//End of while-loop


} //End of main method

Java program help, I have most of it.?

Newbie...newbie...
That's a newbie boo boo.

You need
toString()
because toString() is a method, not a variable.

That's it! Adding () will fix it!

+add
btvec is a vector. It's a collection, not a single value. Did you want to print all the elements of the vector? If so
for( int k=0;kwill do it.

Help with beginner java program?

You've got a number of issues -- some are showstoppers, while others merely have the potential to skew your output.

-Variable "bankfees" is never assigned a value.
-Variable "accountbalance" is defined as a double, but you're forcing your user input into it as an int. That will work...but not as you probably intended, bank accounts being what they are.
-Drop the open/close brackets after your first if statement and final else statement, respectively.
-Add an "else" before your second if statement -- as it stands, your program will run both conditionals as long as numberofchecks is less than 40, which will guarantee that you never see the 0.10 result.
-Your "numberofchecks * X" statements need to be assigned to a variable -- at the moment, you're not doing anything with the result. Variable "bankfees" is probably what you had intended to use for this.
-Drop the semicolon following your first "if" conditional.
-"accountbalance =+ extrafee;" <-- this would be "+=" for syntactical correctness...but you also probably don't intend to directly add that value to the account balance, seeing as it's the *fees* that you're computing for this exercise.

Last item: your final print statement.
-At the moment, it's printing out "Bankfees" followed immediately (no spaces) by three numbers. Throw a set of parentheses around the things you intend to sum together in that statement in order to actually have them added into a single number.
-It's also throwing those numbers (zero [unassigned, remember], 15, and 10) together regardless of user input. The monthly charge and whichever checking fees apply will always be present, but recall that "extrafee" should not *always* be added -- just whenever the pre-checking-fees balance is under 400.

EDIT: James Bond did a good job of catching most of the above in his revision...though at the current time of writing (12:48 AM EDT) it has two minor flaws that can cause an incorrect result, and one that will cause a compile error.

Newbie, need help with java code. ?

Yes, you CAN have more than one method called "main", as long as they have different signatures. Only the one that is public, static, and takes a single String array parameter will be the main entry point for execution.

Now, the first of many problems I see with your code (besides breaking naming conventions), is that you have not declared the variables Weight and Ounces. From your code, I can guess that Weight is supposed to be a String variable, and Ounces is supposed to be an int. Simply declare them as static variables within the class but outside of the methods. But as you will see in my rework of your code, you don't need to keep them at all.

Secondly, your second "main" method has void as the return type, yet is trying to use the return statement to return the value of shipping.

Third, you are using Java I/O classes with methods that throw IOExceptions when something goes wrong. You have to either catch them, or declare them. I don't know what you want to do, so I just declare "throws Exception" and let your thread exit the main method and die. Also note that Integer.parseInt(String s) will throw a NumberFormatException if you give it a String that cannot be parsed into an integer.

Here, I've fixed your code and adjusted names to meet naming conventions.

public class Shipping
{

public static void main(String args[]) throws Exception
{
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
//prints, prompts and get input
System.out.println("SHIPPING COST CALCULATOR");
System.out.println();
System.out.println("Enter your package(s)' weight");
String weight = dataIn.readLine();
int ounces = Integer.parseInt(weight);
//here, I assume you want to immediate calculate the shipping
double shipping = getShipping(ounces);
System.out.println("The shipping charge is: " + shipping);
}

public static double getShipping(int ounces);
{
double shipping = 0;
double basicCharge = 12.95;

if (ounces > 16)
shipping = basicCharge + ((ounces - 16) * .30);
else
shipping = basicCharge;
return shipping;
}
}

Java newbie, help with some code please?

Hi guys. I'm totally new to java, only started a couple days ago so forgive my ignorance. I'm using a book to teach myself and doing all the exercises in it. I'm having a minor problem with one of them that's annoying me.
The program reads a list of 15 numbers and computes the frequency of even numbers. It seems to work fine except that when I try to get it to print "Frequency of even numbers =" + the answer, it prints it several times. I just want it to print one line. I'm sure its just a stupid mistake on my part but its bugging me. here's the code.


import java.util.Scanner;
class Ch3Ex2Q1c{
final static int N = 15;
//N is the number of elements in list
public static void main(String [] args){
int freq, x;
Scanner in = new Scanner(System.in);
System.out.print("Enter 15 numbers: ");
//user input
for(int j = 0; j < N; j = j + 1){
x = in.nextInt();
freq = 0;
//initial state
if (x%2==0)
//only even numbers
freq = freq + 1;
else
; //skip
System.out.println("Frequency of even numbers = " + freq);

Like I said, I'm totally new to this so I'm having trouble pin pointing the problem. Any help appreciated.

The client server modelMost interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.Notice that the client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established.Notice also that once a connection is established, both sides can send and receive information.The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket.A socket is one end of an interprocess communication channel. The two processes each establish their own socket.The steps involved in establishing a socket on the client side are as follows:Create a socket with the socket() system callConnect the socket to the address of the server using the connect() system callSend and receive data. There are a number of ways to do this like send() , recv() but the simplest is to use the read() and write() system calls.The steps involved in establishing a socket on the server side are as follows:Create a socket with the socket() system callBind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.Listen for connections with the listen() system callAccept a connection with the accept() system call. This call typically blocks until a client connects with the server .Send and receive data.Hope this helps:)

Generally, to call your self software developer, you need to start learning what is software development and what is coding which means you need for sure to start learning programming first !after being able to understand and solve basic problems using computer programming, then you can go and learn about data structures and algorithms.Don’t be that pushed or rushed :)Imagine that you went to learn an algorithm called DFS, then you read something like this:Start from an unvisited parent node.Using recursion, go to all your children, mark them as visited, then start DFS again from each one of them.this is a very simple example, a very simple algorithm, but do you think, a person who don’t know what we called here “coding” will be able to understand it ?Algorithms in general are systematic ways to solve problems, but don’t forget that, if you want to solve a problem, you have to e aware of somethings around it, some concepts.In the computational algorithms, you also need to be aware of somethings, I don’t like the term “coding” as it’s not all about writing code, it’s about concepts.So, you really can learn computational algorithms without be able to write code, as long as you can understand concepts like I mentioned above (recursion, graph, node, parent, child), So, you really can start doing algorithms before “coding”, it’s also not available, but I believe it’s the right way to learn algorithms, learn them theoretically (understand them well) then start writing code later after being good enough with the concepts and the thinking in computer algorithms.According to learn “coding” before algorithms, all of us (unfortunately) did that, and I don’t believe it’s the right way, but it will work, as here we are, we did that in the wrong way, but anyway we learned.also see this:Should I learn algorithms first before learning programming? If so, which is the best way to learn algorithms?Can I start learning algorithms before learning programming?Should I learn algorithms before learning programming?To learn coding, should one first learn a language or algorithms?

I am trying to write a Java program to turn some words into mores code and play it with audio. Need help.?

From what I can tell, you are trying to pass a Scanner into the play method, when the play method accepts an int array int[].

So the only solution I have for you is to do similar to what you are doing in the playSine method and create an int array from the file you have in the Scanner.

Without seeing any of your other code, like where you declare the Audio that you are using in the playSine method, and what you are writing when you get the error, I cannot help you any further.

In Python, you don’t need a semicolon when assigning a variable.It’s not Java, and that’s the main source of your error.For you to assign a variable in Python, the correct syntax is variable=value (without semicolon).So your code should look as follows (if I type it, Quora doesn’t maintain indentation, that’s why I’m using a screenshot):Indentation is very important, so pay attention to it.