im actually pretty new into java , my homework was to create a array .
After a while of searching for a good video i found this .
--
If i compile this blue j shows me that "int 1=0;" is not a statement .
Can someone tell me where the mistake is ?
Thanks for helping
import java.util.*;
class binarysearch
{
public static void main ()
{
Scanner sc= new Scanner (System.in);
int num[]= new int [] {23,34,45,56,67,78,89,90,12};
System.out.println("Enter the Number to search");
int n=sc.nextInt();
int u=num.length-1,mid=0;
int 1=0;
int c=0;
while(1<=u)
{
mid=(1+u)/2;
if(n<num[mid])
u=mid-1;
if(n>num[mid])
1=mid+1;
if(n==num[mid])
{
c=1;
break;
}
}
if(c==1)
System.out.println("search successful"+"\n"+"this number " +n+ "position" +(mid+1));
else
System.out.println("Number not found");
}
}
1 is an invalid variable name. With that declaration, you are basically telling Java to re-create math. Thus, int 1 = 0; is impossible to compile.
Consider restating the constructor:
int i = 0;
Related
So I've tried adding count ++ in multiple places in my code along with researching some way to allow multiple inputs to no avail. Am I missing something on placement or would I need to rewrite the code entirely for what I am wanting to accomplish? Is this not a boolean match situation? Very lost and sorry if this is a noob question. Appreciate the input.
Scanner scanner = new Scanner(System.in);
System.out.println("Insert any State Capital");
String currentInput = scanner.nextLine();
boolean match = false;
String [] capitals = stateCapitals[1];
for (String capital:capitals) {
if (capital.equalsIgnoreCase(currentInput)) {
match = true;
break;
}
}
if (match) {
System.out.println ("Correct");
}
else
System.out.println ("incorrect");
}
}
Problem and what I've tried is above. Also, apologies on formatting if it's messed up. First time using stack overflow.
Please see the below code and let me know if this solves your issue.
import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
ArrayList<String> inputList = new ArrayList<>();
ArrayList<String> capitalList = new ArrayList<>(List.of("Delhi","Kabul","Dhaka"));
for (int i = 0; i < 5; i++)
{
String currentInput = scanner.nextLine();
inputList.add(currentInput);
}
int correct_response_count = 0;
int wrong_response_count = 0;
for (int i = 0; i < inputList.size(); i++)
{
if (capitalList.contains(inputList.get(i)))
{
correct_response_count++;
}
else
{
wrong_response_count++;
}
}
System.out.println("The correct count of answers is: " + correct_response_count);
System.out.println("The wrong count of answers is: " + wrong_response_count);
}
}
Input
India
Delhi
Australia
Kabul
Bangladesh
Output
The correct count of answers is: 2
The wrong count of answers is: 3
I keep trying different kinds of code and I always come back to this. but it never seems to work. The last if statement is making the i's underlined red but I can't even understand why. The homework was to make a program that takes user input and put it into an array and see if the user input is already sorted. Please Help!
import java.util.Scanner;
public class Sorting
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the array size: ");
int a = input.nextInt();
System.out.println("Enter the numbers using spaces between each number: ");
int[] numbers = new int[a];
for (int i=0; i<numbers.length; i++)
{
numbers[i]=input.nextInt();
if(isSorted(numbers))
{
System.out.println("Sort is already sorted");
}
else
{
System.out.println("Sort is not sorted sorry");
}
}
}
public static boolean isSorted(int[] numbers)
{
for(int i = 0; i<numbers.length-1; i++);
{
if(numbers[i]>numbers[i+1])
{
return false;
}
}
return true;
}
}
Close the for loop before the if statement.
for(int i = 0; i<numbers.length-1; i++); //<===== remove the ';' here
I think you missed place the ; after the for loop and that cause your issue.
the first line takes the size of the array to follow.The array's digits are checked to see if they can form a ambiguous permutation.
public class Codechef2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int intnum=10;
intnum=input.nextInt();
input.nextLine();
String a[]=new String[100000];
int count=0;
int i=0;
While(intnum>0)
{
a[i]=input.nextLine();
String arr[]=a[i].split(" ");
int aj[]=new int[arr.length];
int k=0;
for(int j=0;j<arr.length;j++)
{
aj[Integer.parseInt(arr[k])]=j+1;
k++;
}
for(int l=0;l<arr.length;l++)
{
if(aj[l]==Integer.parseInt(arr[l]))
count++;
}
if(count==arr.length)
System.out.println("ambiguous permutation");
else
System.out.println("Not ambiguous permutation");
intnum=input.nextInt();
}
}
}
EDIT:
Again: pleas post a compilable code:
//wrong
//While(intnum>0)
while(intnum>0)
Also:
//you need to print a msg so the user knows that he / she
//needs to input and tell the user what to input
System.out.println("Please enter ....");
input.nextLine();
The code has many errors.
For example :
String aj[]=new aj[arr.length]; //will not compile
While(intnum>0) //will not compile
a[i]=input.nextLine(); //will not compile. i is defined later.
aj[arr[k]]=i+1; //wrong : aj[] is a String. i+1 is an int.
Fix them and post a compilable code.
Code below the while loop is not executed until the conditions of the while loop are met.
I have to implement a program using Stack which allows a user to enter a positive integer,n, followed by n integers. Then the program should allow the user to enter another integer, val after which the program should display the last value which is greater than val. From my understanding of the program, I figured out that it should compare each element from the top of the stack to the val. Therefore to compare each element to val, it should iterate through the values in the stack starting from the top. I don't really know how to make this work so would be pleased if I could get any help. Here is my program:
public class MyClass{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
Stack<Integer> NumsInStack= new Stack<Integer>();
int n, num, val;
System.out.println("Please enter n.");
n=sc.nextInt();
for(int i=0; i<n;i++){
num=sc.nextInt();
NumsInStack.push(num);
}
System.out.println("Enter a value.");
val=sc.nextInt();
for(int i=0; i<NumsInStack.size();i++){
if(NumsInStack.peek()>val)
System.out.println("The number greater than "+val+" is "+NumsInStack.peek());
}
}
}
You should never iterate over a stack, it defeats the entire purpose of choosing that data structure. You want to use pop and peek:
while (!NumsInStack.empty()) {
if (NumsInStack.peek() > val) {
System.out.println("woot!");
break;
}
NumsInStack.pop();
}
Since you only want it to print out the last number that is higher, you should also put a break; after the print statement so that it breaks out of the loop when it finds a match. Without the break it would print all of the values that are higher.
Working code:
import java.util.*;
public class MyClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> NumsInStack = new Stack<Integer>();
int n, num, val;
System.out.println("Please enter n.");
n = sc.nextInt();
for (int i = 0; i < n; i++) {
num = sc.nextInt();
NumsInStack.push(num);
}
System.out.println("Enter a value.");
val = sc.nextInt();
while ( !NumsInStack.empty()){
int stackElement = NumsInStack.pop();
if ( stackElement > val){
System.out.println("Stack Element > value :"+stackElement+":"+val);
break;
}
}
}
}
I am VERY new to Java (ie week 2 of AP Computer Science A). we have to use JOptionPane to make message boxes to use with math operations. My code was fine at school but I had to reformat it because I saved it on Facebook (no other option). Now I get errors on all the JOptionPane lines saying "Multiple markers at this line" and " Syntax error on tokens".
How do I fix that. here is the code (please only fix what I am asking, nothing else, I know the code is probably weird)
import javax.swing.JOptionPane;
public class OptioPane {
public static void main(String[] args) {
}
String a = JOptionPane.showInputDialog("Enter an integer");
String b = JOptionPane.showInputDialog("Input another");
int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
String c = JOptionPane.showInputDialog("Enter an integer");
String d = JOptionPane.showInputDialog("Input another");
int f = Integer.parseInt(c);
int g = Integer.parseInt(d);
JOptionPane.showMessageDialog(null, "The second number subtracted from the first number is " +(f-g));
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
String k = JOptionPane.showInputDialog("Enter an integer");
String j = JOptionPane.showInputDialog("Input another");
int n = Integer.parseInt(a);
int m = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number divided by the second number is " +(n/m));
String fir = JOptionPane.showInputDialog("Enter an integer");
String tir = JOptionPane.showInputDialog("Input another");
int ah = Integer.parseInt(a);
int bh = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number modulated by the second number is " +(ah*bh));
}
Basically, the body of your code is out side of any executable context (not where it belongs)....
public class OptioPane {
public static void main(String[] args) {
}
/* All your stuff - out of bounds and behaving badly */
}
Instead, you need to place this code within a executable context, such as the main method...
public class OptioPane {
public static void main(String[] args) {
/* All your stuff - playing nicely */
}
}
Oh, add the this line...
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
Is wrong, see the extra g in Dialog, it should be...
JOptionPane.showMessageDialog(null, "The numbers added together is " +(x+y));
It seems that you did something wrong while re-formatting the code because the entire code block should be inside the curly braces of the main method
I saw your comment about multiplication and if you still have problems with it, it's because of this
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
when you input 5 into 's' and 5 into 'r' and get 30, you probably had 5 and 6 in 'a' and 'b' respectively.
Try this:
import javax.swing.JOptionPane;
public class Dialogue1 {
public static void main(String[]arg)
{
String number1,number2;
number1=JOptionPane.showInputDialog("enter first number");
number2=JOptionPane.showInputDialog("enter second number");
int num1=Integer.parseInt(number1);
int num2=Integer.parseInt(number2);
int sum=num1+num2;
String message=String.format("the sum is %d",sum);
JOptionPane.showMessageDialog(null,sum);
}
}
Store your result in another variable and then display it. It will work for sure without any error.
If you face any issue I guess there are some suggestions given below the error in Eclipse. I have also encountered the same error it suggested me to import the java swing package then it got fixed.