Syntax Error, Insert } to Complete Block [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
import java.util.*;
public class Programming {
public static void main(String[] args) {
//Scanner scan = new Scanner( System.in );
int l=0;
StringBuilder password = new StringBuilder();
public static boolean matchCharAt(StringBuilder password, int l){
l = password.length();
if (l < 0 || l > 100){
return false;
}
for (int i = 0; i < password.length();i++){
if (!Character.isLetter(password.charAt(l)))
return false;
}
return true;
}
}
It says I have an error on the line with { for (l=0; l < 100; l++); }, but i'm not sure if thats where the Curly brace error is. It might not be a curly brace error, i'm unsure, but I was hoping I could get some help to not have this error.

You never close the main() method block:
public static void main(String[] args) {
//Scanner scan = new Scanner( System.in );
int l=0;
StringBuilder password = new StringBuilder();
{
for (l = 0; l < 100; l++);
}
} //HERE!
Besides this loop:
for (l = 0; l < 100; l++);
is not doing anything except changing the value of l to 100. Also the loop is surrounded with a block that has no practical sense. I can only guess this is what you wanted:
for (l = 0; l < 100; l++) {
matchCharAt(password, l);
//...
}

This is corollary to your main problem, but you are also changing an argument in this method
public static boolean matchCharAt(StringBuilder password, int l){
l = password.length();
if (l < 0 || l > 100){
return false;
}
If you're passing in l, then you're going to be changing its value with l = password.length().

I'm not sure exactly what you want. This should at least compile:
public class SomeClass {
public static void main(String[] args) {
//Scanner scan = new Scanner( System.in );
int l=0;
StringBuilder password = new StringBuilder();
for (l = 0; l < 100; l++) {
; // Does nothing...
}
} // end of "main()"
public static boolean matchCharAt(StringBuilder password, int l){
l = password.length();
if (l < 0 || l > 100){
return false;
}
for (int i = 0; i < password.length();i++){
if (!Character.isLetter(password.charAt(l)))
return false;
}
return true;
} // end of "matchCharAt()"
} // end of class

Related

My Java project doesn't print out anything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my code:
public class Code {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
reverse(-123);
}
public static int reverse(int x) {
String str = Integer.toString(x);
char c[] = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
c[i] = str.charAt(str.length() - 1 - i);
}
if (c[str.length()-1] == '-') {
for (int i=c.length-2; i >=0; i--) {
c[i+1] = c[i];
}
c[0] = '-';
}
String n = new String(c);
int re = Integer.parseInt(n);
return re;
}
}
I'm doing a Java exercise which is to change the order of an Integer to reverse with the '-' in it, but when I run the program, it doesn't print out anything, please help me solve this!!
public static void main(String[] args) {
System.out.println(reverse(-123));
}
should work now, you missed print statement.
To print we need to add a print statement i.e,
System.out.println(reverse(-123));
Just add System.out.println where you called the function reverse
public class Code {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(reverse(-123));
}
public static int reverse(int x) {
String str = Integer.toString(x);
char c[] = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
c[i] = str.charAt(str.length() - 1 - i);
}
if (c[str.length()-1] == '-') {
for (int i=c.length-2; i >=0; i--) {
c[i+1] = c[i];
}
c[0] = '-';
}
String n = new String(c);
int re = Integer.parseInt(n);
return re;
}
}

Java - NumberFormatException when using .parseInt(String)

I am trying to run a loop to see if an int is sorted. however the int has to be converted from a string. here is my code.
public static void main(String[] args) {
// TODO code application logic here
Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
System.out.println("enter the max value of ordered squares:");
int max = maxVal.nextInt();
for(int i = 0; i*i <= max; i++){
int L = String.valueOf(i*i).length();
String sq = String.valueOf(i*i);
String [] digits = new String[L];
for(int a = 0; a < L; a++){
digits [a] = Character.toString(sq.charAt(a));
if(L == 1){
System.out.print(sq + "");
}else if(Integer.parseInt(digits [a]) < Integer.parseInt(digits[a+1])){
System.out.print(sq);
}else{
}
}
}
}
when I run it, I get an error :
Exception in thread "main" java.lang.NumberFormatException: null
0149 at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
why does Integer.parseInt() not work
Your problem is that digits[a+1] hasn't been defined yet. I see that on line 2 you have
digits[a] = Character.toString(sq.charAt(a));
and you're iterating over a in a for loop, so I daresay that digits[a+1] hasn't been assigned yet.
UPDATE 1
Check out this solution, it shows how to properly catch that exception and how to avoid it:
Java: Good way to encapsulate Integer.parseInt()
UPDATE 2
I decided to add a fixed version of your code:
public static void main(String[] args) {
// TODO code application logic here
Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
System.out.println("enter the max value of ordered squares:");
int max = maxVal.nextInt();
for(int i = 0; i*i <= max; i++){
int L = String.valueOf(i*i).length();
String sq = String.valueOf(i*i);
String [] digits = new String[L];
for(int a = 0; a < L; a++){
digits [a] = Character.toString(sq.charAt(a));
if(L == 1 || a == 0){
System.out.print(sq + "");
}else if(Integer.parseInt(digits [a]) < Integer.parseInt(digits[a+1])){
System.out.print(sq);
}else{
}
}
}
}
While I don't know the utility of your code, but this implementation might be simpler:
public static void main(String[] args) {
// TODO code application logic here
Scanner maxVal = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
System.out.println("enter the max value of ordered squares:");
int max = maxVal.nextInt();
for(int i = 0; i*i <= max; i++){
long sq = i*i;
if(sq > 9){
String[] digits = sq.toString().split("");
//Notice that I start at index 1, so I can do [a-1] safely
for(int a = 1; a < digits.length; a++){
if(Integer.parseInt(digits [a-1]) < Integer.parseInt(digits[a])){
System.out.print(sq);
//I guess we don't want a number like 169 (13*13) to be displayed twice, so:
break;
}
}
} else {
System.out.print(sq);
}
}
}

for loop ( Java) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am making a guessing game for fun. For some reason, the code in the for loop never processes. I didvided it into three parts. Please let me knowCan someone help me? I have checked and the code doesnt proceed int the for loop. I am certain there is nothing wrong with the for loop. Thanks for your hwlp Thanks
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Guess {
public static Random r = new Random();
public static BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
public static final String YES_S = "y";
public static final String NO_S = "n";
public static void main(String [] args) throws IOException {
boolean menu = true;
boolean start = false;
boolean end = false;
boolean ans = true;
boolean rand = true;
int num = -1;
int guessNum = -1;
while(menu) {
System.out.println( "Start game [ y ]:");
String input = in.readLine();
if(input.equals(YES_S)) {
menu = false;
start = true;
end = false;
}
}
while(start) {
while(ans) {
while(rand) {
num = r.nextInt(11);
rand = false;
}
for (int i = 0; i > 3; i++) {
System.out.println( " Guess a number from 0 to 10 :");
String input1 = in.readLine();
guessNum = Integer.parseInt(input1);
if (guessNum == num) {
System.out.println( " Congratulations !");
ans = false;
rand = true;
} else {
System.out.println( " Try again");
}
}
if(ans = true) {
end = true;
}
}
}
}
}
Well, let's decompose your for loop:
for (int i = 0; i > 3; i++)
Start with i = 0
Perform the body while i > 3 .... whops, do you see the problem?
for (int i = 0; i > 3; i++) {
The guard condition is never true - 0 > 3 is false immediately, so the loop never runs.
Use i < 3 as the guard instead.
You have i>3 and i=0... 0 is not greater than 3. So, this is wrong! Change to this:
for (int i = 0; i < 3; i++) {
System.out.println( " Guess a number from 0 to 10 :");
String input1 = in.readLine();
guessNum = Integer.parseInt(input1);
if (guessNum == num) {
System.out.println( " Congratulations !");
ans = false;
rand = true;
} else {
System.out.println( " Try again");
}
}
Check that if(ans = true) { is an assigment and not a check condition
if you want to check the value of ans do
if(ans == true) {
OR EVEN BETTER
if(ans) {
the other reason why is not working is because this:
for (int i = 0; i > 3; i++) {
the condition is never met
do instead
for (int i = 0; i < 3; i++) {
Flip your comparison operator:
for (int i = 0; i < 3; i++) {

Sort an array list of pathnames in java without comparator [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My user input is
\home\me\cs1
\usr\share
\var\log
\usr\local\jdk1.6.0\jre\lib
and I need to sort these pathnames so that the output is in the correct lexographic order. However they are first sorted by length which is the number of slashes in each string. The path names are stored in an arraylist of strings. I attempting to do this without the use of collections,comparator or arrays. Would this be possible with the use of ArrayList?
the output should be:
\usr\share
\var\log
\home\me\cs1
\usr\local\jdk1.6.0\jre\lib
This is my code so far:
import java.util.ArrayList;
import java.util.Scanner;
public class FileName
{
private ArrayList<String> pathNames;
public FileName()
{
pathNames = new ArrayList<String>();
}
public void printPaths()
{
for(int i = 0; i < pathNames.size(); i++)
{
System.out.println(pathNames.get(i));
}
}
public int pathLength(String path)
{
int count = 0;
String slash = "\\";
for(int i = 0; i < path.length(); i++)
{
if(path.substring(i,i + 1).equals(slash))
{
count++;
}
}
return count;
}
public void sort()
{
pathNames = mergeSort(pathNames);
}
public ArrayList<String> mergeSort(ArrayList<String> paths)
{
if(paths.size() == 1)
{
return paths;
}
else
{
ArrayList<String> left = new ArrayList<String>();
ArrayList<String> right = new ArrayList<String>();
int middle = paths.size() / 2;
for(int i = 0; i < middle; i++)
{
left.add(paths.get(i));
}
for(int i = middle; i < paths.size(); i++)
{
right.add(paths.get(i));
}
right = mergeSort(left);
left = mergeSort(left);
merge(left, right, paths);
}
return paths;
}
public void merge(ArrayList<String> left, ArrayList<String> right, ArrayList<String> paths)
{
int leftNum = 0;
int rightNum = 0;
int pathsNum = 0;
while (leftNum < left.size() && rightNum < right.size())
{
if ((left.get(leftNum).compareTo(right.get(rightNum)))<0)
{
paths.set(pathsNum, left.get(leftNum));
leftNum++;
}
else
{
paths.set(pathsNum, right.get(rightNum));
rightNum++;
}
pathsNum++;
}
ArrayList<String>rest;
int restNum;
if (leftNum >= left.size())
{
rest = right;
restNum = rightNum;
}
else
{
rest = left;
restNum = leftNum;
}
for (int i = restNum; i < rest.size(); i++)
{
paths.set(pathsNum, rest.get(i));
pathsNum++;
}
}
public void readInput()
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a list of path names.(press enter after each path name, and type \"stop\" once you are finished.");
String termination = "stop";
String in = input.nextLine();
boolean reading = true;
while(reading)
{
pathNames.add(in);
if(in.equals(termination))
{
reading = false;
return;
}
in = input.nextLine();
}
}
}
This is my main method.
public class FileNamePrgm
{
public static void main(String [] args)
{
FileName paths = new FileName();
paths.readInput();
paths.sort();
}
}
You have a typo:
right = mergeSort(left);
Should be
right = mergeSort(right);
Also you need to add in = input.nextLine(); once more inside the while loop. Currently you are reading only one line from the input and checking it over and over again.

Null pointer exception on line 40 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need a program that expands a string into single lines.
Operators "\|" and "," delimit colums.
IN
a10,a2|||||f4|g5|h107||j1|k13,k3||||
OUT
a10|||||f4|g5|h107||j1|k13|
a10|||||f4|g5|h107||j1|k3|
a2|||||f4|g5|h107||j1|k13|
a2|||||f4|g5|h107||j1|k3|
import java.io.*;
public class CitesteLinii{
static String[] expandedLine = null;
static String[][] lineToExpand = null;
static int N = 0;
static String toLine() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i< N; i++) {
if (i>0) {
sb.append("|");
}
String[] list = expandedLine;
for (int pi = 0; pi <list.length;pi++) {
if (pi>0) {
sb.append(",");
}
sb.append(list[pi]);
}
}
return sb.toString();
}
static void expandLine(int i) {
if (i == N) {
System.out.print(toLine());
}
else {
String[] v = lineToExpand[i];
if (v == null || v.length == 0) {
expandedLine[i] = "";
expandLine(i + 1);
} else {
for (int p = 0; p < v.length; p++) {
expandedLine[i]=v[p];
expandLine(i + 1);
}
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new FileReader(new File("in.txt")));
try {
String line = in.readLine();
while (line!=null) {
String[] parts = line.split("\\|");
N= parts.length;
lineToExpand = new String[N][];
for (int ai = 0; ai < N; ai++) {
String[] v = parts[ai].split(",");
System.out.println(parts[ai]+' ');
lineToExpand[ai] = v;
}
expandLine(0);
line = in.readLine();
}
}
finally {
in.close();
}
}
}
expandedLine is not initialised anywhere (except for null). Thus, the access to the elements yields a null pointer exception.
Adding to #Howard explanation .. you need to initialize the expandedLine array before you add anything to it . .so you must do something like
String[] expandedLine = new String[100]; before using expandedLine[i]=v[p]; The number 100 inside the new String[..] statment defined the size of the array, you need to decide

Categories